You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
crate/log/logsdk/logtext/option.go

70 lines
1.3 KiB
Go

package logtext
import (
"io"
"time"
"git.blauwelle.com/go/crate/log/logsdk/logjson"
)
type Option interface {
apply(cfg *config)
}
func WithBufferPool(pool logjson.BytesBufferPool) Option {
return optionFunc(func(cfg *config) {
cfg.bytesBufferPool = pool
})
}
func WithOutput(w io.Writer) Option {
return optionFunc(func(cfg *config) {
cfg.output = w
})
}
func WithTimeFormat(format string) Option {
return optionFunc(func(cfg *config) {
cfg.timeFormat = format
cfg.timePadding = len(format)
})
}
func WithDisableTime(disable bool) Option {
return optionFunc(func(cfg *config) {
cfg.disableTime = disable
})
}
type config struct {
bytesBufferPool logjson.BytesBufferPool
output io.Writer
timeFormat string
timePadding int
disableTime bool
}
func newConfig(opts ...Option) *config {
cfg := new(config)
for _, opt := range opts {
opt.apply(cfg)
}
if cfg.bytesBufferPool == nil {
cfg.bytesBufferPool = logjson.DefaultBytesBufferPool
}
if cfg.output == nil {
cfg.output = logjson.DefaultStderrSyncWriter
}
if cfg.timeFormat == "" {
cfg.timeFormat = time.RFC3339Nano
cfg.timePadding = len(cfg.timeFormat)
}
return cfg
}
type optionFunc func(cfg *config)
func (fn optionFunc) apply(cfg *config) {
fn(cfg)
}