Skip to content

Commit

Permalink
use custom labels for front attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdf committed Mar 15, 2024
1 parent df37e3b commit 5570804
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
11 changes: 11 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ func (h namedHandler) Handle(ctx context.Context, record slog.Record) error {
opts := &slog.HandlerOptions{
AddSource: config.EnableSource,
Level: leveler,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
switch a.Key {
case slog.MessageKey, slog.LevelKey, slog.TimeKey:
// ignore these built in attributes
// so that we can customize the order
return slog.Attr{}
case slog.SourceKey:
a.Key = "$source"
}
return a
},
}

var output io.Writer
Expand Down
9 changes: 7 additions & 2 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,17 @@ func (l *Logger) log(ctx context.Context, level slog.Level, err error, msg strin
}

r := slog.NewRecord(time.Now(), level, msg, pcs[0])
r.Add(slog.Any("name", l.name))
// add front attributes in specific order
r.Add(slog.String("$time", r.Time.Format(time.DateTime)))
r.Add(slog.String("$name", l.name))
r.Add(slog.Any("$level", r.Level))
r.Add(slog.String("$msg", r.Message))
// add remaining attributes
r.AddAttrs(args...)

// add stack trace if enabled
if err != nil && config.EnableStackTrace {
r.Add("stacktrace", fmt.Sprintf("%+v", err))
r.Add("$stack", fmt.Sprintf("%+v", err))
}

_ = l.handler.Handle(ctx, r)
Expand Down
19 changes: 15 additions & 4 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ func TestLoggerLogWithConfigOverride(t *testing.T) {
assert.Equal(t, "test", handler.records[0].Message)

attrs := []slog.Attr{
slog.Any("name", "test"),
slog.Any("$name", "test"),
slog.Any("$level", slog.LevelInfo),
slog.Any("$msg", "test"),
slog.Any("arg1", "val1"),
slog.Any("stacktrace", fmt.Sprintf("%+v", err)),
slog.Any("$stack", fmt.Sprintf("%+v", err)),
}
assertRecordAttrs(t, handler.records[0], attrs...)
}
Expand All @@ -71,7 +73,14 @@ func TestLoggerInfoWithEnableSource(t *testing.T) {
assert.Equal(t, slog.LevelInfo, handler.records[0].Level)
assert.Equal(t, "test", handler.records[0].Message)
assert.NotEqual(t, uintptr(0x00), handler.records[0].PC)
assertRecordAttrs(t, handler.records[0], slog.Any("name", "test"), slog.Any("arg1", "val1"))

attrs := []slog.Attr{
slog.Any("$name", "test"),
slog.Any("$level", slog.LevelInfo),
slog.Any("$msg", "test"),
slog.Any("arg1", "val1"),
}
assertRecordAttrs(t, handler.records[0], attrs...)
}

func TestLoggerWithAttrs(t *testing.T) {
Expand Down Expand Up @@ -109,7 +118,9 @@ func assertRecordAttrs(
) {
var actual []slog.Attr
record.Attrs(func(a slog.Attr) bool {
actual = append(actual, a)
if a.Key != "$time" {
actual = append(actual, a)
}
return true
})
assert.Equal(t, expected, actual)
Expand Down

0 comments on commit 5570804

Please sign in to comment.