From 2f41f3243c68d34571140a1cbaff8770121a8cae Mon Sep 17 00:00:00 2001 From: Aleksandr Kliuev Date: Wed, 10 Jul 2024 16:13:24 +0200 Subject: [PATCH 1/2] Preserve the 'err' Attribute Name We do not always have control over how attribute names are defined. However, it is beneficial to highlight errors in development logs for better visibility. --- handler.go | 6 +++--- handler_test.go | 10 +++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/handler.go b/handler.go index 62d153c..d54b5d7 100644 --- a/handler.go +++ b/handler.go @@ -345,7 +345,7 @@ func (h *handler) appendAttr(buf *buffer, attr slog.Attr, groupsPrefix string, g } } else if err, ok := attr.Value.Any().(tintError); ok { // append tintError - h.appendTintError(buf, err, groupsPrefix) + h.appendTintError(buf, err, attr.Key, groupsPrefix) buf.WriteByte(' ') } else { h.appendKey(buf, attr.Key, groupsPrefix) @@ -395,9 +395,9 @@ func (h *handler) appendValue(buf *buffer, v slog.Value, quote bool) { } } -func (h *handler) appendTintError(buf *buffer, err error, groupsPrefix string) { +func (h *handler) appendTintError(buf *buffer, err error, attrKey, groupsPrefix string) { buf.WriteStringIf(!h.noColor, ansiBrightRedFaint) - appendString(buf, groupsPrefix+errKey, true) + appendString(buf, groupsPrefix+attrKey, true) buf.WriteByte('=') buf.WriteStringIf(!h.noColor, ansiResetFaint) appendString(buf, err.Error(), true) diff --git a/handler_test.go b/handler_test.go index 8e710fd..de32abc 100644 --- a/handler_test.go +++ b/handler_test.go @@ -271,6 +271,14 @@ func TestHandler(t *testing.T) { }, Want: `Nov 10 23:00:00.000 ERR test err=`, }, + { // preserve the error attribute name + F: func(l *slog.Logger) { + te := tint.Err(errors.New("something bad happens")) + te.Key = "Error" + l.Error("test", te) + }, + Want: `Nov 10 23:00:00.000 ERR test Error="something bad happens"`, + }, { // https://github.com/lmittmann/tint/pull/26 Opts: &tint.Options{ ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { @@ -326,7 +334,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:327 test`, + Want: `Nov 10 23:00:00.000 INF tint/handler_test.go:335 test`, }, { // https://github.com/lmittmann/tint/issues/44 F: func(l *slog.Logger) { From ece0c057b8dad80ef0896eb88f6ba745d2970e09 Mon Sep 17 00:00:00 2001 From: lmittmann Date: Wed, 10 Jul 2024 18:02:48 +0200 Subject: [PATCH 2/2] make testcase consistent with the test suite --- handler_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/handler_test.go b/handler_test.go index de32abc..737f2d0 100644 --- a/handler_test.go +++ b/handler_test.go @@ -271,14 +271,6 @@ func TestHandler(t *testing.T) { }, Want: `Nov 10 23:00:00.000 ERR test err=`, }, - { // preserve the error attribute name - F: func(l *slog.Logger) { - te := tint.Err(errors.New("something bad happens")) - te.Key = "Error" - l.Error("test", te) - }, - Want: `Nov 10 23:00:00.000 ERR test Error="something bad happens"`, - }, { // https://github.com/lmittmann/tint/pull/26 Opts: &tint.Options{ ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { @@ -334,7 +326,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:335 test`, + Want: `Nov 10 23:00:00.000 INF tint/handler_test.go:327 test`, }, { // https://github.com/lmittmann/tint/issues/44 F: func(l *slog.Logger) { @@ -352,6 +344,14 @@ func TestHandler(t *testing.T) { }, Want: `Nov 10 23:00:00.000 INF test key="{A:123 B:}"`, }, + { // https://github.com/lmittmann/tint/pull/66 + F: func(l *slog.Logger) { + errAttr := tint.Err(errors.New("fail")) + errAttr.Key = "error" + l.Error("test", errAttr) + }, + Want: `Nov 10 23:00:00.000 ERR test error=fail`, + }, } for i, test := range tests {