Skip to content

Commit 19710fc

Browse files
authored
fix: Stdlog writer should output with the logger level (#129)
* fix: Stdlog writer should output with the logger level * Fix level * Add level argument * Fix test level * Update example
1 parent 5c0525b commit 19710fc

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func ExampleWith() {
111111

112112
func ExampleStdlib() {
113113
ctx := slog.With(context.Background(), slog.F("field", 1))
114-
l := slog.Stdlib(ctx, slog.Make(sloghuman.Sink(os.Stdout)))
114+
l := slog.Stdlib(ctx, slog.Make(sloghuman.Sink(os.Stdout)), slog.LevelInfo)
115115

116116
l.Print("msg")
117117

s.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,31 @@ import (
88

99
// Stdlib creates a standard library logger from the given logger.
1010
//
11-
// All logs will be logged at the Info level and the given ctx
12-
// will be passed to the logger's Info method, thereby logging
13-
// all fields and tracing info in the context.
11+
// All logs will be logged at the level set by the logger and the
12+
// given ctx will be passed to the logger's Log method, thereby
13+
// logging all fields and tracing info in the context.
1414
//
1515
// You can redirect the stdlib default logger with log.SetOutput
1616
// to the Writer on the logger returned by this function.
1717
// See the example.
18-
func Stdlib(ctx context.Context, l Logger) *log.Logger {
19-
l.skip += 3
18+
func Stdlib(ctx context.Context, l Logger, level Level) *log.Logger {
19+
l.skip += 2
2020

2121
l = l.Named("stdlib")
2222

2323
w := &stdlogWriter{
24-
ctx: ctx,
25-
l: l,
24+
ctx: ctx,
25+
l: l,
26+
level: level,
2627
}
2728

2829
return log.New(w, "", 0)
2930
}
3031

3132
type stdlogWriter struct {
32-
ctx context.Context
33-
l Logger
33+
ctx context.Context
34+
l Logger
35+
level Level
3436
}
3537

3638
func (w stdlogWriter) Write(p []byte) (n int, err error) {
@@ -39,7 +41,7 @@ func (w stdlogWriter) Write(p []byte) (n int, err error) {
3941
// we do not want.
4042
msg = strings.TrimSuffix(msg, "\n")
4143

42-
w.l.Info(w.ctx, msg)
44+
w.l.log(w.ctx, w.level, msg, Map{})
4345

4446
return len(p), nil
4547
}

s_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestStdlib(t *testing.T) {
1717
l := slog.Make(sloghuman.Sink(b)).With(
1818
slog.F("hi", "we"),
1919
)
20-
stdlibLog := slog.Stdlib(bg, l)
20+
stdlibLog := slog.Stdlib(bg, l, slog.LevelInfo)
2121
stdlibLog.Println("stdlib")
2222

2323
et, rest, err := entryhuman.StripTimestamp(b.String())

sloggers/slogtest/t.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
// Ensure all stdlib logs go through slog.
2020
func init() {
2121
l := slog.Make(sloghuman.Sink(os.Stderr))
22-
log.SetOutput(slog.Stdlib(context.Background(), l).Writer())
22+
log.SetOutput(slog.Stdlib(context.Background(), l, slog.LevelInfo).Writer())
2323
}
2424

2525
// Options represents the options for the logger returned

0 commit comments

Comments
 (0)