diff --git a/log/logsdk/logjson/option.go b/log/logsdk/logjson/option.go new file mode 100644 index 0000000..ac0a2bb --- /dev/null +++ b/log/logsdk/logjson/option.go @@ -0,0 +1,98 @@ +package logjson + +import ( + "io" + "os" + "time" + + "git.blauwelle.com/go/crate/synchelper" +) + +// Option 配置日志处理对象 +type Option interface { + apply(cfg *config) +} + +// WithBufferPool 配置缓冲池 +func WithBufferPool(pool synchelper.BytesBufferPool) Option { + return optionFunc(func(cfg *config) { + cfg.pool = pool + }) +} + +// WithOutput 配置输出 +func WithOutput(w io.Writer) Option { + return optionFunc(func(cfg *config) { + cfg.output = w + }) +} + +// WithTimeFormat 配置时间格式 +func WithTimeFormat(format string) Option { + return optionFunc(func(cfg *config) { + cfg.timestampFormat = format + }) +} + +// WithDisableTime 配置仅用时间输出 +func WithDisableTime(disable bool) Option { + return optionFunc(func(cfg *config) { + cfg.disableTime = disable + }) +} + +// WithDisableHTMLEscape 配置禁止 HTML 转义 +func WithDisableHTMLEscape(disable bool) Option { + return optionFunc(func(cfg *config) { + cfg.disableHTMLEscape = disable + }) +} + +// WithPrettyPrint 配置 JSON 多行缩进输出 +func WithPrettyPrint(pretty bool) Option { + return optionFunc(func(cfg *config) { + cfg.prettyPrint = pretty + }) +} + +func newConfig(opts ...Option) *config { + cfg := defaultConfig() + for _, opt := range opts { + opt.apply(cfg) + } + if !cfg.hasPool { + cfg.pool = synchelper.NewBytesBufferPool(512, 4096) + } + if cfg.output == nil { + cfg.output = synchelper.NewSyncWriter(os.Stderr) + } + if cfg.timestampFormat == "" { + cfg.timestampFormat = time.RFC3339Nano + } + return cfg +} + +func defaultConfig() *config { + return &config{ + hasPool: false, + disableTime: false, + disableHTMLEscape: false, + prettyPrint: false, + } +} + +type config struct { + pool synchelper.BytesBufferPool + output io.Writer + timestampFormat string + hasPool bool + disableTime bool + disableHTMLEscape bool + prettyPrint bool +} + +type optionFunc func(cfg *config) + +func (fn optionFunc) apply(cfg *config) { + fn(cfg) +} diff --git a/log/logsdk/jsonlog/processor.go b/log/logsdk/logjson/processor.go similarity index 57% rename from log/logsdk/jsonlog/processor.go rename to log/logsdk/logjson/processor.go index 3da6e57..eede248 100644 --- a/log/logsdk/jsonlog/processor.go +++ b/log/logsdk/logjson/processor.go @@ -1,11 +1,10 @@ -package jsonlog +package logjson import ( "encoding/json" "fmt" "io" "os" - "time" "git.blauwelle.com/go/crate/log/logsdk" "git.blauwelle.com/go/crate/runtimehelper" @@ -17,10 +16,7 @@ var _ logsdk.EntryProcessor = &Processor{} // New 返回日志处理对象, // 返回的对象是 [logsdk.EntryProcessor]. func New(opts ...Option) *Processor { - cfg := defaultConfig() - for _, opt := range opts { - opt.apply(cfg) - } + cfg := newConfig(opts...) return &Processor{ bufferPool: cfg.pool, output: cfg.output, @@ -82,53 +78,6 @@ func (processor *Processor) Process(entry logsdk.ReadonlyEntry) { } } -// Option 配置日志处理对象 -type Option interface { - apply(cfg *config) -} - -// WithBufferPool 配置缓冲池 -func WithBufferPool(pool synchelper.BytesBufferPool) Option { - return optionFunc(func(cfg *config) { - cfg.pool = pool - }) -} - -// WithOutput 配置输出 -func WithOutput(w io.Writer) Option { - return optionFunc(func(cfg *config) { - cfg.output = w - }) -} - -// WithTimeFormat 配置时间格式 -func WithTimeFormat(format string) Option { - return optionFunc(func(cfg *config) { - cfg.timestampFormat = format - }) -} - -// WithDisableTime 配置仅用时间输出 -func WithDisableTime(disable bool) Option { - return optionFunc(func(cfg *config) { - cfg.disableTime = disable - }) -} - -// WithDisableHTMLEscape 配置禁止 HTML 转义 -func WithDisableHTMLEscape(disable bool) Option { - return optionFunc(func(cfg *config) { - cfg.disableHTMLEscape = disable - }) -} - -// WithPrettyPrint 配置 JSON 多行缩进输出 -func WithPrettyPrint(pretty bool) Option { - return optionFunc(func(cfg *config) { - cfg.prettyPrint = pretty - }) -} - // Entry 被用来 JSON 序列化 type Entry struct { Message string `json:"msg"` @@ -138,29 +87,3 @@ type Entry struct { Fields []logsdk.Field `json:"fields,omitempty"` Level logsdk.Level `json:"level"` } - -func defaultConfig() *config { - return &config{ - pool: synchelper.NewBytesBufferPool(512, 2048), - output: synchelper.NewSyncWriter(os.Stderr), - timestampFormat: time.RFC3339Nano, - disableTime: false, - disableHTMLEscape: false, - prettyPrint: false, - } -} - -type config struct { - pool synchelper.BytesBufferPool - output io.Writer - timestampFormat string - disableTime bool - disableHTMLEscape bool - prettyPrint bool -} - -type optionFunc func(cfg *config) - -func (fn optionFunc) apply(cfg *config) { - fn(cfg) -}