From be892f7512da57e879d6356e40cfde2c204cd116 Mon Sep 17 00:00:00 2001 From: Inhere Date: Sun, 24 Mar 2024 23:45:41 +0800 Subject: [PATCH] :necktie: up: add TextFormatterWith for quickly make a textFormatter --- formatter_test.go | 2 +- formatter_text.go | 50 ++++++++++++++++++++++++++++++++--------------- issues_test.go | 10 ++++++---- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/formatter_test.go b/formatter_test.go index b8dab92..9c32f66 100644 --- a/formatter_test.go +++ b/formatter_test.go @@ -69,7 +69,7 @@ func TestNewTextFormatter(t *testing.T) { t.Run("CallerFormatFunc", func(t *testing.T) { buf := byteutil.NewBuffer() h := handler.IOWriterWithMaxLevel(buf, slog.DebugLevel) - h.SetFormatter(slog.NewTextFormatter().Configure(func(f *slog.TextFormatter) { + h.SetFormatter(slog.TextFormatterWith(func(f *slog.TextFormatter) { f.CallerFormatFunc = func(rf *runtime.Frame) string { return "custom_caller" } diff --git a/formatter_text.go b/formatter_text.go index 8bae47a..a7272a4 100644 --- a/formatter_text.go +++ b/formatter_text.go @@ -46,8 +46,14 @@ type TextFormatter struct { EncodeFunc func(v any) string // CallerFormatFunc the caller format layout. default is defined by CallerFlag CallerFormatFunc CallerFormatFn + + // TODO BeforeFunc call it before format, update fields or other + // BeforeFunc func(r *Record) } +// TextFormatterFn definition +type TextFormatterFn func(*TextFormatter) + // NewTextFormatter create new TextFormatter func NewTextFormatter(template ...string) *TextFormatter { var fmtTpl string @@ -59,8 +65,8 @@ func NewTextFormatter(template ...string) *TextFormatter { f := &TextFormatter{ // default options - TimeFormat: DefaultTimeFormat, ColorTheme: ColorTheme, + TimeFormat: DefaultTimeFormat, // EnableColor: color.SupportColor(), // EncodeFunc: func(v any) string { // return fmt.Sprint(v) @@ -72,9 +78,21 @@ func NewTextFormatter(template ...string) *TextFormatter { return f } +// TextFormatterWith create new TextFormatter with options +func TextFormatterWith(fns ...TextFormatterFn) *TextFormatter { + return NewTextFormatter().WithOptions(fns...) +} + // Configure the formatter -func (f *TextFormatter) Configure(fn func(*TextFormatter)) *TextFormatter { - fn(f) +func (f *TextFormatter) Configure(fn TextFormatterFn) *TextFormatter { + return f.WithOptions(fn) +} + +// WithOptions func on the formatter +func (f *TextFormatter) WithOptions(fns ...TextFormatterFn) *TextFormatter { + for _, fn := range fns { + fn(f) + } return f } @@ -112,6 +130,7 @@ var textPool bytebufferpool.Pool // //goland:noinspection GoUnhandledErrorResult func (f *TextFormatter) Format(r *Record) ([]byte, error) { + f.beforeFormat() buf := textPool.Get() defer textPool.Put(buf) @@ -158,15 +177,15 @@ func (f *TextFormatter) Format(r *Record) ([]byte, error) { } case field == FieldKeyData: if f.FullDisplay || len(r.Data) > 0 { - buf.WriteString(f.encodeVal(r.Data)) + buf.WriteString(f.EncodeFunc(r.Data)) } case field == FieldKeyExtra: if f.FullDisplay || len(r.Extra) > 0 { - buf.WriteString(f.encodeVal(r.Extra)) + buf.WriteString(f.EncodeFunc(r.Extra)) } default: if _, ok := r.Fields[field]; ok { - buf.WriteString(f.encodeVal(r.Fields[field])) + buf.WriteString(f.EncodeFunc(r.Fields[field])) } else { buf.WriteString(field) } @@ -177,20 +196,19 @@ func (f *TextFormatter) Format(r *Record) ([]byte, error) { return buf.B, nil } -func (f *TextFormatter) encodeVal(v any) string { - if f.EncodeFunc != nil { - return f.EncodeFunc(v) +func (f *TextFormatter) beforeFormat() { + // if f.BeforeFunc == nil {} + if f.EncodeFunc == nil { + f.EncodeFunc = EncodeToString } - return EncodeToString(v) -} - -func (f *TextFormatter) renderColorByLevel(text string, level Level) string { if f.ColorTheme == nil { f.ColorTheme = ColorTheme } +} - if theme, ok := f.ColorTheme[level]; ok { - return theme.Render(text) +func (f *TextFormatter) renderColorByLevel(s string, l Level) string { + if theme, ok := f.ColorTheme[l]; ok { + return theme.Render(s) } - return text + return s } diff --git a/issues_test.go b/issues_test.go index 6e828d0..0eb5ea5 100644 --- a/issues_test.go +++ b/issues_test.go @@ -137,13 +137,15 @@ func TestIssues_139(t *testing.T) { // }) h1 := handler.NewConsoleHandler(slog.AllLevels) h1.SetFormatter(textFormatter) - ctx := context.WithValue(context.Background(), "requestid", "111111") L := slog.New() L.AddHandlers(h1) // add processor <==== - L.AddProcessor(slog.ProcessorFunc(func(r *slog.Record) { - r.Fields["requestid"] = r.Ctx.Value("requestid") - })) + // L.AddProcessor(slog.ProcessorFunc(func(r *slog.Record) { + // r.Fields["requestid"] = r.Ctx.Value("requestid") + // })) + L.AddProcessor(slog.AppendCtxKeys("requestid")) + + ctx := context.WithValue(context.Background(), "requestid", "111111") L.WithCtx(ctx).Info("test") }