Skip to content

Commit

Permalink
Fixed wrong level type in ReplaceAttr (#40)
Browse files Browse the repository at this point in the history
* added test cases

* fixed wrong level type in `ReplaceAttr`

---------

Co-authored-by: lmittmann <[email protected]>
  • Loading branch information
lmittmann and lmittmann committed Sep 14, 2023
1 parent f895639 commit ccda5f8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
35 changes: 17 additions & 18 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,8 @@ func (h *handler) Handle(_ context.Context, r slog.Record) error {
if rep == nil {
h.appendLevel(buf, r.Level)
buf.WriteByte(' ')
} else if a := rep(nil /* groups */, slog.Int(slog.LevelKey, int(r.Level))); a.Key != "" {
if a.Value.Kind() == slog.KindInt64 {
h.appendLevel(buf, slog.Level(a.Value.Int64()))
} else {
h.appendValue(buf, a.Value, false)
}
} else if a := rep(nil /* groups */, slog.Any(slog.LevelKey, r.Level)); a.Key != "" {
h.appendValue(buf, a.Value, false)
buf.WriteByte(' ')
}

Expand Down Expand Up @@ -293,36 +289,37 @@ func (h *handler) appendTime(buf *buffer, t time.Time) {
}

func (h *handler) appendLevel(buf *buffer, level slog.Level) {
delta := func(buf *buffer, val slog.Level) {
if val == 0 {
return
}
buf.WriteByte('+')
*buf = strconv.AppendInt(*buf, int64(val), 10)
}

switch {
case level < slog.LevelInfo:
buf.WriteString("DBG")
delta(buf, level-slog.LevelDebug)
appendLevelDelta(buf, level-slog.LevelDebug)
case level < slog.LevelWarn:
buf.WriteStringIf(!h.noColor, ansiBrightGreen)
buf.WriteString("INF")
delta(buf, level-slog.LevelInfo)
appendLevelDelta(buf, level-slog.LevelInfo)
buf.WriteStringIf(!h.noColor, ansiReset)
case level < slog.LevelError:
buf.WriteStringIf(!h.noColor, ansiBrightYellow)
buf.WriteString("WRN")
delta(buf, level-slog.LevelWarn)
appendLevelDelta(buf, level-slog.LevelWarn)
buf.WriteStringIf(!h.noColor, ansiReset)
default:
buf.WriteStringIf(!h.noColor, ansiBrightRed)
buf.WriteString("ERR")
delta(buf, level-slog.LevelError)
appendLevelDelta(buf, level-slog.LevelError)
buf.WriteStringIf(!h.noColor, ansiReset)
}
}

func appendLevelDelta(buf *buffer, delta slog.Level) {
if delta == 0 {
return
} else if delta > 0 {
buf.WriteByte('+')
}
*buf = strconv.AppendInt(*buf, int64(delta), 10)
}

func (h *handler) appendSource(buf *buffer, src *slog.Source) {
dir, file := filepath.Split(src.File)

Expand Down Expand Up @@ -387,6 +384,8 @@ func (h *handler) appendValue(buf *buffer, v slog.Value, quote bool) {
appendString(buf, v.Time().String(), quote)
case slog.KindAny:
switch cv := v.Any().(type) {
case slog.Level:
h.appendLevel(buf, cv)
case encoding.TextMarshaler:
data, err := cv.MarshalText()
if err != nil {
Expand Down
27 changes: 25 additions & 2 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ func TestHandler(t *testing.T) {
},
Want: `Nov 10 23:00:00.000 INF+1 test`,
},
{
Opts: &tint.Options{
Level: slog.LevelDebug - 1,
},
F: func(l *slog.Logger) {
l.Log(context.TODO(), slog.LevelDebug-1, "test")
},
Want: `Nov 10 23:00:00.000 DBG-1 test`,
},
{ // https://github.com/lmittmann/tint/issues/12
F: func(l *slog.Logger) {
l.Error("test", slog.Any("error", errors.New("fail")))
Expand Down Expand Up @@ -292,7 +301,21 @@ func TestHandler(t *testing.T) {
},
Want: `group.key=val`,
},
{ // https://github.com/lmittmann/tint/pull/37
{ // https://github.com/lmittmann/tint/issues/36
Opts: &tint.Options{
ReplaceAttr: func(g []string, a slog.Attr) slog.Attr {
if len(g) == 0 && a.Key == slog.LevelKey {
_ = a.Value.Any().(slog.Level)
}
return a
},
},
F: func(l *slog.Logger) {
l.Info("test")
},
Want: `Nov 10 23:00:00.000 INF test`,
},
{ // https://github.com/lmittmann/tint/issues/37
Opts: &tint.Options{
AddSource: true,
ReplaceAttr: func(g []string, a slog.Attr) slog.Attr {
Expand All @@ -302,7 +325,7 @@ func TestHandler(t *testing.T) {
F: func(l *slog.Logger) {
l.Info("test")
},
Want: `Nov 10 23:00:00.000 INF tint/handler_test.go:303 test`,
Want: `Nov 10 23:00:00.000 INF tint/handler_test.go:326 test`,
},
}

Expand Down

0 comments on commit ccda5f8

Please sign in to comment.