File tree Expand file tree Collapse file tree 4 files changed +15
-13
lines changed Expand file tree Collapse file tree 4 files changed +15
-13
lines changed Original file line number Diff line number Diff line change @@ -111,7 +111,7 @@ func ExampleWith() {
111
111
112
112
func ExampleStdlib () {
113
113
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 )
115
115
116
116
l .Print ("msg" )
117
117
Original file line number Diff line number Diff line change @@ -8,29 +8,31 @@ import (
8
8
9
9
// Stdlib creates a standard library logger from the given logger.
10
10
//
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.
14
14
//
15
15
// You can redirect the stdlib default logger with log.SetOutput
16
16
// to the Writer on the logger returned by this function.
17
17
// 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
20
20
21
21
l = l .Named ("stdlib" )
22
22
23
23
w := & stdlogWriter {
24
- ctx : ctx ,
25
- l : l ,
24
+ ctx : ctx ,
25
+ l : l ,
26
+ level : level ,
26
27
}
27
28
28
29
return log .New (w , "" , 0 )
29
30
}
30
31
31
32
type stdlogWriter struct {
32
- ctx context.Context
33
- l Logger
33
+ ctx context.Context
34
+ l Logger
35
+ level Level
34
36
}
35
37
36
38
func (w stdlogWriter ) Write (p []byte ) (n int , err error ) {
@@ -39,7 +41,7 @@ func (w stdlogWriter) Write(p []byte) (n int, err error) {
39
41
// we do not want.
40
42
msg = strings .TrimSuffix (msg , "\n " )
41
43
42
- w .l .Info (w .ctx , msg )
44
+ w .l .log (w .ctx , w . level , msg , Map {} )
43
45
44
46
return len (p ), nil
45
47
}
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ func TestStdlib(t *testing.T) {
17
17
l := slog .Make (sloghuman .Sink (b )).With (
18
18
slog .F ("hi" , "we" ),
19
19
)
20
- stdlibLog := slog .Stdlib (bg , l )
20
+ stdlibLog := slog .Stdlib (bg , l , slog . LevelInfo )
21
21
stdlibLog .Println ("stdlib" )
22
22
23
23
et , rest , err := entryhuman .StripTimestamp (b .String ())
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ import (
19
19
// Ensure all stdlib logs go through slog.
20
20
func init () {
21
21
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 ())
23
23
}
24
24
25
25
// Options represents the options for the logger returned
You can’t perform that action at this time.
0 commit comments