Skip to content

Commit

Permalink
👔 up: add TextFormatterWith for quickly make a textFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 24, 2024
1 parent b683bb6 commit be892f7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
2 changes: 1 addition & 1 deletion formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
50 changes: 34 additions & 16 deletions formatter_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
}

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
10 changes: 6 additions & 4 deletions issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit be892f7

Please sign in to comment.