Skip to content

fix: Stdlog writer should output with the logger level #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
22 changes: 12 additions & 10 deletions s.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,31 @@ 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
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) {
Expand All @@ -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.Info(w.ctx, msg)
w.l.log(w.ctx, w.level, msg, Map{})

return len(p), nil
}
2 changes: 1 addition & 1 deletion s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion sloggers/slogtest/t.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down