From 6696f42eda75b19db91b745eba7b88f0df4e5909 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Mon, 8 Nov 2021 23:35:48 +0000 Subject: [PATCH 1/5] fix: Stdlog writer should output with the logger level --- s.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/s.go b/s.go index fa8917e..0101ffe 100644 --- a/s.go +++ b/s.go @@ -39,7 +39,7 @@ func (w stdlogWriter) Write(p []byte) (n int, err error) { // we do not want. msg = strings.TrimSuffix(msg, "\n") - w.l.Info(w.ctx, msg) + w.l.log(w.ctx, w.l.level, msg, Map{}) return len(p), nil } From f808c13f1ed0a3fa03357e8fb7349fcd35a1d645 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Tue, 9 Nov 2021 00:18:06 +0000 Subject: [PATCH 2/5] Fix level --- s.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/s.go b/s.go index 0101ffe..b09a554 100644 --- a/s.go +++ b/s.go @@ -8,15 +8,15 @@ import ( // Stdlib creates a standard library logger from the given logger. // -// All logs will be logged at the Info level and the given ctx -// will be passed to the logger's Info method, thereby logging -// all fields and tracing info in the context. +// All logs will be logged at the level set by the logger and the +// given ctx will be passed to the logger's Log method, thereby +// logging all fields and tracing info in the context. // // You can redirect the stdlib default logger with log.SetOutput // to the Writer on the logger returned by this function. // See the example. func Stdlib(ctx context.Context, l Logger) *log.Logger { - l.skip += 3 + l.skip += 2 l = l.Named("stdlib") From 5c47cf3ccc326de56158cc9264796fb3cb3f0a33 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Tue, 9 Nov 2021 00:25:11 +0000 Subject: [PATCH 3/5] Add level argument --- s.go | 14 ++++++++------ s_test.go | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/s.go b/s.go index b09a554..250a003 100644 --- a/s.go +++ b/s.go @@ -15,22 +15,24 @@ import ( // You can redirect the stdlib default logger with log.SetOutput // to the Writer on the logger returned by this function. // See the example. -func Stdlib(ctx context.Context, l Logger) *log.Logger { +func Stdlib(ctx context.Context, l Logger, level Level) *log.Logger { l.skip += 2 l = l.Named("stdlib") w := &stdlogWriter{ - ctx: ctx, - l: l, + ctx: ctx, + l: l, + level: level, } return log.New(w, "", 0) } type stdlogWriter struct { - ctx context.Context - l Logger + ctx context.Context + l Logger + level Level } func (w stdlogWriter) Write(p []byte) (n int, err error) { @@ -39,7 +41,7 @@ func (w stdlogWriter) Write(p []byte) (n int, err error) { // we do not want. msg = strings.TrimSuffix(msg, "\n") - w.l.log(w.ctx, w.l.level, msg, Map{}) + w.l.log(w.ctx, w.level, msg, Map{}) return len(p), nil } diff --git a/s_test.go b/s_test.go index b79e2b6..7ca9fa9 100644 --- a/s_test.go +++ b/s_test.go @@ -17,7 +17,7 @@ func TestStdlib(t *testing.T) { l := slog.Make(sloghuman.Sink(b)).With( slog.F("hi", "we"), ) - stdlibLog := slog.Stdlib(bg, l) + stdlibLog := slog.Stdlib(bg, l, slog.LevelInfo) stdlibLog.Println("stdlib") et, rest, err := entryhuman.StripTimestamp(b.String()) From 3c8051d072ce02623563cb66a9684b6ac2bb06c8 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Tue, 9 Nov 2021 00:30:10 +0000 Subject: [PATCH 4/5] Fix test level --- sloggers/slogtest/t.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sloggers/slogtest/t.go b/sloggers/slogtest/t.go index 3b056ed..568d401 100644 --- a/sloggers/slogtest/t.go +++ b/sloggers/slogtest/t.go @@ -19,7 +19,7 @@ import ( // Ensure all stdlib logs go through slog. func init() { l := slog.Make(sloghuman.Sink(os.Stderr)) - log.SetOutput(slog.Stdlib(context.Background(), l).Writer()) + log.SetOutput(slog.Stdlib(context.Background(), l, slog.LevelInfo).Writer()) } // Options represents the options for the logger returned From 1da86bb19bc615e39031f738e471cabf053bab37 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Tue, 9 Nov 2021 01:07:01 +0000 Subject: [PATCH 5/5] Update example --- example_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_test.go b/example_test.go index c5e84ce..2853ff7 100644 --- a/example_test.go +++ b/example_test.go @@ -111,7 +111,7 @@ func ExampleWith() { func ExampleStdlib() { ctx := slog.With(context.Background(), slog.F("field", 1)) - l := slog.Stdlib(ctx, slog.Make(sloghuman.Sink(os.Stdout))) + l := slog.Stdlib(ctx, slog.Make(sloghuman.Sink(os.Stdout)), slog.LevelInfo) l.Print("msg")