Skip to content

Commit

Permalink
fix: support custom slog levels (#117)
Browse files Browse the repository at this point in the history
* fix: type cast slog level to int32 instead of static map lookup

* test: add tests for custom slog levels

* fix: type cast level in <1.21 logger
  • Loading branch information
lvlcn-t authored Apr 22, 2024
1 parent d23bea6 commit 2b8ec72
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 34 deletions.
15 changes: 0 additions & 15 deletions level_121.go

This file was deleted.

15 changes: 0 additions & 15 deletions level_no121.go

This file was deleted.

4 changes: 2 additions & 2 deletions logger_121.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
//
// Implements slog.Handler.
func (l *Logger) Enabled(_ context.Context, level slog.Level) bool {
return atomic.LoadInt32(&l.level) <= int32(fromSlogLevel[level])
return atomic.LoadInt32(&l.level) <= int32(level)
}

// Handle handles the Record. It will only be called if Enabled returns true.
Expand All @@ -33,7 +33,7 @@ func (l *Logger) Handle(ctx context.Context, record slog.Record) error {
// Get the caller frame using the record's PC.
frames := runtime.CallersFrames([]uintptr{record.PC})
frame, _ := frames.Next()
l.handle(fromSlogLevel[record.Level], l.timeFunc(record.Time), []runtime.Frame{frame}, record.Message, fields...)
l.handle(Level(record.Level), l.timeFunc(record.Time), []runtime.Frame{frame}, record.Message, fields...)
return nil
}

Expand Down
34 changes: 34 additions & 0 deletions logger_121_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package log

import (
"bytes"
"context"
"testing"
"time"

"log/slog"

Expand Down Expand Up @@ -149,3 +151,35 @@ func TestSlogWithGroup(t *testing.T) {
})
}
}

func TestSlogCustomLevel(t *testing.T) {
var buf bytes.Buffer
cases := []struct {
name string
expected string
level slog.Level
minLevel Level
}{
{
name: "custom level not enabled",
expected: "",
level: slog.Level(500),
minLevel: Level(600),
},
{
name: "custom level enabled",
expected: "foo\n",
level: slog.Level(500),
minLevel: Level(100),
},
}
for _, c := range cases {
buf.Reset()
t.Run(c.name, func(t *testing.T) {
l := New(&buf)
l.SetLevel(c.minLevel)
l.Handle(context.Background(), slog.NewRecord(time.Now(), c.level, "foo", 0))
assert.Equal(t, c.expected, buf.String())
})
}
}
4 changes: 2 additions & 2 deletions logger_no121.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
//
// Implements slog.Handler.
func (l *Logger) Enabled(_ context.Context, level slog.Level) bool {
return atomic.LoadInt32(&l.level) <= int32(fromSlogLevel[level])
return atomic.LoadInt32(&l.level) <= int32(level)
}

// Handle handles the Record. It will only be called if Enabled returns true.
Expand All @@ -30,7 +30,7 @@ func (l *Logger) Handle(_ context.Context, record slog.Record) error {
// Get the caller frame using the record's PC.
frames := runtime.CallersFrames([]uintptr{record.PC})
frame, _ := frames.Next()
l.handle(fromSlogLevel[record.Level], l.timeFunc(record.Time), []runtime.Frame{frame}, record.Message, fields...)
l.handle(Level(record.Level), l.timeFunc(record.Time), []runtime.Frame{frame}, record.Message, fields...)
return nil
}

Expand Down

0 comments on commit 2b8ec72

Please sign in to comment.