From 010c941ae288ffeda80b7a4591c2d613eaa4deb5 Mon Sep 17 00:00:00 2001 From: Kajetan Date: Mon, 6 Mar 2023 00:18:11 +0100 Subject: [PATCH 1/2] feat: custom logger format --- config.go | 15 +++++++++++++ custom_format_encoder.go | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 custom_format_encoder.go diff --git a/config.go b/config.go index 65587ee..3733c09 100644 --- a/config.go +++ b/config.go @@ -84,6 +84,8 @@ type Config struct { // See Open for details. Output []string `mapstructure:"output"` + Format string `mapstructure:"format"` + // ErrorOutput is a list of URLs to write internal logger errors to. // The default is standard error. // @@ -194,6 +196,19 @@ func (cfg *Config) BuildLogger() (*zap.Logger, error) { zCfg.Encoding = cfg.Encoding } + if cfg.Format != "" { + err := zap.RegisterEncoder("custom-format", func(config zapcore.EncoderConfig) (zapcore.Encoder, error) { + return &customFormatEncoder{ + Encoder: zapcore.NewConsoleEncoder(config), + format: cfg.Format, + }, nil + }) + if err != nil { + return nil, err + } + zCfg.Encoding = "custom-format" + } + if len(cfg.Output) != 0 { zCfg.OutputPaths = cfg.Output } diff --git a/custom_format_encoder.go b/custom_format_encoder.go new file mode 100644 index 0000000..2e01c5c --- /dev/null +++ b/custom_format_encoder.go @@ -0,0 +1,48 @@ +package logger + +import ( + "fmt" + "strings" + + "go.uber.org/zap/buffer" + "go.uber.org/zap/zapcore" +) + +type customFormatEncoder struct { + format string + zapcore.Encoder +} + +func (e *customFormatEncoder) Clone() zapcore.Encoder { + return &customFormatEncoder{Encoder: e.Encoder.Clone()} +} + +func (e *customFormatEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) { + str := e.format + replace := map[string]string{ + "%level_name%": entry.Level.CapitalString(), + "%message%": entry.Message, + "%time%": fmt.Sprint(entry.Time.UnixMilli()), + "%stack%": entry.Stack, + "%caller_file%": entry.Caller.File, + "%caller_function%": entry.Caller.Function, + } + + for s, r := range replace { + str = strings.Replace(str, s, r, -1) + } + + var contextValues []string + for _, v := range fields { + contextValues = append(contextValues, v.String) + } + + str = strings.Replace(str, "%context%", strings.Join(contextValues, " "), -1) + + pool := buffer.NewPool() + buf := pool.Get() + buf.AppendString(str) + buf.AppendByte('\n') + + return buf, nil +} From 32c03d12719cd1b98112fb03ce9de1fab7d35231 Mon Sep 17 00:00:00 2001 From: Kajetan Date: Mon, 6 Mar 2023 00:25:14 +0100 Subject: [PATCH 2/2] feat: custom logger format --- custom_format_encoder.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/custom_format_encoder.go b/custom_format_encoder.go index 2e01c5c..2a1f771 100644 --- a/custom_format_encoder.go +++ b/custom_format_encoder.go @@ -29,15 +29,15 @@ func (e *customFormatEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore. } for s, r := range replace { - str = strings.Replace(str, s, r, -1) + str = strings.ReplaceAll(str, s, r) } - var contextValues []string + var contextValues = make([]string, 0, len(fields)) for _, v := range fields { contextValues = append(contextValues, v.String) } - str = strings.Replace(str, "%context%", strings.Join(contextValues, " "), -1) + str = strings.ReplaceAll(str, "%context%", strings.Join(contextValues, " ")) pool := buffer.NewPool() buf := pool.Get()