diff --git a/handler.go b/handler.go index d431628..61049fd 100644 --- a/handler.go +++ b/handler.go @@ -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 diff --git a/logger.go b/logger.go index 3704e8c..8566fcb 100644 --- a/logger.go +++ b/logger.go @@ -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) } diff --git a/logger_test.go b/logger_test.go index 55a0d95..27b35d6 100644 --- a/logger_test.go +++ b/logger_test.go @@ -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...) }