Skip to content

Commit

Permalink
slog: Don't log if not enabled at level (#103)
Browse files Browse the repository at this point in the history
* slog: Don't log if not enabled at level

* Update logger_121.go

* chore: gofmt

---------

Co-authored-by: Ayman Bagabas <[email protected]>
  • Loading branch information
imjasonh and aymanbagabas authored Feb 5, 2024
1 parent f2cb6b6 commit 7a3834f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
6 changes: 2 additions & 4 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions logger_121.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -21,7 +20,11 @@ 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
}

fields := make([]interface{}, 0, record.NumAttrs()*2)
record.Attrs(func(a slog.Attr) bool {
fields = append(fields, a.Key, a.Value.String())
Expand Down

0 comments on commit 7a3834f

Please sign in to comment.