Closed suntong closed 1 year ago
tint
and slog
support custom levels. See e.g. https://github.com/lmittmann/tint/blob/main/handler_test.go#L246-L251 or the slog doc. If you want tint
to write custom level labels beyond e.g. INF+1
you can use Options.ReplaceAttr
.
OK, @lmittmann, I'm trying to do my custom level labels with the code in OP, but,
if a.Key == slog.LevelKey {
level := a.Value.Any().(slog.Level)
the above code works with slog.HandlerOptions
but fails with tint's tint.Options.ReplaceAttr
:
panic: interface conversion: interface {} is int64, not slog.Level
Would you show me how exactly to make use of tint.Options.ReplaceAttr
and a.Value
please?
The panic indicates that you are asserting a wrong type for the level. But you are right that there should not be a difference in the type of the level passed to ReplaceAttr
between tint
and slog
.
Please consider adding more log levels by default, as I used to have more than one debug levels, just like we can use multiple
-v
forssh
to get different levels of debug info.I'm proposing:
Which would give us:
NB, I tried to put the above into tint, but got:
panic: interface conversion: interface {} is int64, not slog.Level
at my
logger.Log(ctx, LevelDbg1
line.Ref: https://betterstack.com/community/guides/logging/logging-in-go/#creating-custom-log-levels