Skip to content

Commit

Permalink
Merge pull request #4 from sourcenetwork/nasdf/feat/error-attribute
Browse files Browse the repository at this point in the history
feat: Add error attribute
  • Loading branch information
nasdf authored Jun 18, 2024
2 parents bfd0a2c + 4d97ba5 commit 2b56d11
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const (
nameKey = "$name"
// stackKey is the key for the logger stack attribute
stackKey = "$stack"
// errorKey is the key for the logger error attribute
errorKey = "$err"
// msgKey is the key for the logger message attribute
msgKey = "$msg"
// timeKey is the key for the logger time attribute
Expand Down
4 changes: 4 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func (l *Logger) log(ctx context.Context, level slog.Level, err error, msg strin
if err != nil && config.EnableStackTrace {
r.Add(stackKey, fmt.Sprintf("%+v", err))
}
// add error if not nil
if err != nil {
r.Add(errorKey, err.Error())
}

_ = l.handler.Handle(ctx, r)
}
1 change: 1 addition & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestLoggerLogWithConfigOverride(t *testing.T) {
slog.Any(nameKey, "test"),
slog.Any("arg1", "val1"),
slog.Any(stackKey, fmt.Sprintf("%+v", err)),
slog.Any(errorKey, err.Error()),
}
assertRecordAttrs(t, handler.records[0], attrs...)
}
Expand Down

0 comments on commit 2b56d11

Please sign in to comment.