From 049bd2cd50de62ec22fe8d1fc0888739f059dca6 Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Mon, 5 Feb 2024 16:54:59 -0500 Subject: [PATCH 1/3] slog: Don't log if not enabled at level --- logger_121.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/logger_121.go b/logger_121.go index dc1b14d..7388e6c 100644 --- a/logger_121.go +++ b/logger_121.go @@ -22,6 +22,10 @@ func (l *Logger) Enabled(_ context.Context, level slog.Level) bool { // // Implements slog.Handler. func (l *Logger) Handle(_ context.Context, record slog.Record) error { + if !l.Enabled(ctx, record.Level) { + return nil + } + fields := make([]interface{}, 0, record.NumAttrs()*2) record.Attrs(func(a slog.Attr) bool { fields = append(fields, a.Key, a.Value.String()) From 54870dd9cef3890e8d666c0ab96d12020c7eaca4 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Mon, 5 Feb 2024 17:01:16 -0500 Subject: [PATCH 2/3] Update logger_121.go --- logger_121.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logger_121.go b/logger_121.go index 7388e6c..ee0f4f8 100644 --- a/logger_121.go +++ b/logger_121.go @@ -21,7 +21,7 @@ func (l *Logger) Enabled(_ context.Context, level slog.Level) bool { // Handle handles the Record. It will only be called if Enabled returns true. // // Implements slog.Handler. -func (l *Logger) Handle(_ context.Context, record slog.Record) error { +func (l *Logger) Handle(ctx context.Context, record slog.Record) error { if !l.Enabled(ctx, record.Level) { return nil } From 4e2b80b01755493af60b79d4b7ea482752faa562 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Mon, 5 Feb 2024 17:06:24 -0500 Subject: [PATCH 3/3] chore: gofmt --- logger.go | 6 ++---- logger_121.go | 5 ++--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/logger.go b/logger.go index 3249c1a..046da40 100644 --- a/logger.go +++ b/logger.go @@ -15,10 +15,8 @@ import ( "github.com/muesli/termenv" ) -var ( - // ErrMissingValue is returned when a key is missing a value. - ErrMissingValue = fmt.Errorf("missing value") -) +// ErrMissingValue is returned when a key is missing a value. +var ErrMissingValue = fmt.Errorf("missing value") // LoggerOption is an option for a logger. type LoggerOption = func(*Logger) diff --git a/logger_121.go b/logger_121.go index ee0f4f8..6930efd 100644 --- a/logger_121.go +++ b/logger_121.go @@ -5,10 +5,9 @@ package log import ( "context" + "log/slog" "runtime" "sync/atomic" - - "log/slog" ) // Enabled reports whether the logger is enabled for the given level. @@ -25,7 +24,7 @@ func (l *Logger) Handle(ctx context.Context, record slog.Record) error { if !l.Enabled(ctx, record.Level) { return nil } - + fields := make([]interface{}, 0, record.NumAttrs()*2) record.Attrs(func(a slog.Attr) bool { fields = append(fields, a.Key, a.Value.String())