Skip to content

Commit

Permalink
🐛 fix: fix nil panic on use &slog.TextFormatter{} create formatter .s…
Browse files Browse the repository at this point in the history
…ee issues #139
  • Loading branch information
inhere committed Mar 11, 2024
1 parent 1361ac8 commit 6e2c5d4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
18 changes: 15 additions & 3 deletions formatter_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type TextFormatter struct {
// FullDisplay Whether to display when record.Data, record.Extra, etc. are empty
FullDisplay bool
// EncodeFunc data encode for Record.Data, Record.Extra, etc.
//
// Default is encode by EncodeToString()
EncodeFunc func(v any) string
// CallerFormatFunc the caller format layout. default is defined by CallerFlag
Expand Down Expand Up @@ -157,15 +158,15 @@ func (f *TextFormatter) Format(r *Record) ([]byte, error) {
}
case field == FieldKeyData:
if f.FullDisplay || len(r.Data) > 0 {
buf.WriteString(f.EncodeFunc(r.Data))
buf.WriteString(f.encodeVal(r.Data))
}
case field == FieldKeyExtra:
if f.FullDisplay || len(r.Extra) > 0 {
buf.WriteString(f.EncodeFunc(r.Extra))
buf.WriteString(f.encodeVal(r.Extra))
}
default:
if _, ok := r.Fields[field]; ok {
buf.WriteString(f.EncodeFunc(r.Fields[field]))
buf.WriteString(f.encodeVal(r.Fields[field]))
} else {
buf.WriteString(field)
}
Expand All @@ -176,7 +177,18 @@ 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)
}
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)
}
Expand Down
24 changes: 24 additions & 0 deletions issues_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slog_test

import (
"context"
"fmt"
"sync"
"testing"
Expand Down Expand Up @@ -123,3 +124,26 @@ func TestIssues_108(t *testing.T) {
assert.StrContains(t, str, "[probe] [INFO")
assert.StrContains(t, str, "[probe] [WARN")
}

// https://github.com/gookit/slog/issues/139
// 自定义模板报 invalid memory address or nil pointer dereference #139
func TestIssues_139(t *testing.T) {
myTemplate := "[{{datetime}}] [{{requestid}}] [{{level}}] {{message}}\n"
textFormatter := &slog.TextFormatter{TimeFormat: "2006-01-02 15:04:05.000"}
textFormatter.SetTemplate(myTemplate)
// use func create
// textFormatter := slog.NewTextFormatter(myTemplate).Configure(func(f *slog.TextFormatter) {
// f.TimeFormat = "2006-01-02 15:04:05.000"
// })
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.WithCtx(ctx).Info("test")
}

0 comments on commit 6e2c5d4

Please sign in to comment.