From 1840681201266e94c322dce2d5cca1ddebcd6119 Mon Sep 17 00:00:00 2001 From: Ge Song Date: Mon, 10 Apr 2023 13:50:17 +0800 Subject: [PATCH] =?UTF-8?q?log/=E4=BF=AE=E6=94=B9=E5=8C=85=E5=90=8D=20json?= =?UTF-8?q?log->logjson;=20=E4=BF=AE=E6=94=B9=20logjson.config=20=E7=9A=84?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E6=96=B9=E5=BC=8F;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- log/logsdk/logjson/option.go | 98 ++++++++++++++++++++ log/logsdk/{jsonlog => logjson}/processor.go | 81 +--------------- 2 files changed, 100 insertions(+), 79 deletions(-) create mode 100644 log/logsdk/logjson/option.go rename log/logsdk/{jsonlog => logjson}/processor.go (57%) 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) -}