Skip to content

Commit 552693c

Browse files
authored
feat: modify slog.Logger to *not* implement slog.Sink (#95)
The prior implementation allowed slog.Logger instances to themselves be passes as slog.Sink targets. This enabled many usage patterns that introduced extremely counter-intuitive behavior.
1 parent 676a685 commit 552693c

15 files changed

+42
-68
lines changed

example_helper_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func httpLogHelper(ctx context.Context, status int) {
1717
)
1818
}
1919

20-
var l = sloghuman.Make(os.Stdout)
20+
var l = slog.Make(sloghuman.Sink(os.Stdout))
2121

2222
func ExampleHelper() {
2323
ctx := context.Background()

example_marshaller_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (s myStruct) MarshalJSON() ([]byte, error) {
2121
}
2222

2323
func Example_marshaller() {
24-
l := sloghuman.Make(os.Stdout)
24+
l := slog.Make(sloghuman.Sink(os.Stdout))
2525

2626
l.Info(context.Background(), "wow",
2727
slog.F("myStruct", myStruct{

example_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
)
1919

2020
func Example() {
21-
log := sloghuman.Make(os.Stdout)
21+
log := slog.Make(sloghuman.Sink(os.Stdout))
2222

2323
log.Info(context.Background(), "my message here",
2424
slog.F("field_name", "something or the other"),
@@ -45,7 +45,7 @@ func Example() {
4545
}
4646

4747
func Example_struct() {
48-
l := sloghuman.Make(os.Stdout)
48+
l := slog.Make(sloghuman.Sink(os.Stdout))
4949

5050
type hello struct {
5151
Meow int `json:"meow"`
@@ -76,7 +76,7 @@ func Example_testing() {
7676
}
7777

7878
func Example_tracing() {
79-
log := sloghuman.Make(os.Stdout)
79+
log := slog.Make(sloghuman.Sink(os.Stdout))
8080

8181
ctx, _ := trace.StartSpan(context.Background(), "spanName")
8282

@@ -86,14 +86,14 @@ func Example_tracing() {
8686
}
8787

8888
func Example_multiple() {
89-
l := sloghuman.Make(os.Stdout)
89+
l := slog.Make(sloghuman.Sink(os.Stdout))
9090

9191
f, err := os.OpenFile("stackdriver", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
9292
if err != nil {
9393
l.Fatal(context.Background(), "failed to open stackdriver log file", slog.Error(err))
9494
}
9595

96-
l = slog.Make(l, slogstackdriver.Make(f))
96+
l = l.AppendSinks(slogstackdriver.Sink(f))
9797

9898
l.Info(context.Background(), "log to stdout and stackdriver")
9999

@@ -103,15 +103,15 @@ func Example_multiple() {
103103
func ExampleWith() {
104104
ctx := slog.With(context.Background(), slog.F("field", 1))
105105

106-
l := sloghuman.Make(os.Stdout)
106+
l := slog.Make(sloghuman.Sink(os.Stdout))
107107
l.Info(ctx, "msg")
108108

109109
// 2019-12-07 20:54:23.986 [INFO] <example_test.go:20> msg {"field": 1}
110110
}
111111

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

116116
l.Print("msg")
117117

@@ -121,7 +121,7 @@ func ExampleStdlib() {
121121
func ExampleLogger_Named() {
122122
ctx := context.Background()
123123

124-
l := sloghuman.Make(os.Stdout)
124+
l := slog.Make(sloghuman.Sink(os.Stdout))
125125
l = l.Named("http")
126126
l.Info(ctx, "received request", slog.F("remote address", net.IPv4(127, 0, 0, 1)))
127127

@@ -131,7 +131,7 @@ func ExampleLogger_Named() {
131131
func ExampleLogger_Leveled() {
132132
ctx := context.Background()
133133

134-
l := sloghuman.Make(os.Stdout)
134+
l := slog.Make(sloghuman.Sink(os.Stdout))
135135
l.Debug(ctx, "testing1")
136136
l.Info(ctx, "received request")
137137

go.sum

-28
Large diffs are not rendered by default.

s_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestStdlib(t *testing.T) {
1414
t.Parallel()
1515

1616
b := &bytes.Buffer{}
17-
l := slog.Make(sloghuman.Make(b)).With(
17+
l := slog.Make(sloghuman.Sink(b)).With(
1818
slog.F("hi", "we"),
1919
)
2020
stdlibLog := slog.Stdlib(bg, l)

slog.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ type Sink interface {
2929
Sync()
3030
}
3131

32-
// LogEntry logs the given entry with the context to the
32+
// Log logs the given entry with the context to the
3333
// underlying sinks.
3434
//
3535
// It extends the entry with the set fields and names.
36-
func (l Logger) LogEntry(ctx context.Context, e SinkEntry) {
36+
func (l Logger) Log(ctx context.Context, e SinkEntry) {
3737
if e.Level < l.level {
3838
return
3939
}
@@ -138,17 +138,19 @@ func (l Logger) Named(name string) Logger {
138138
func (l Logger) Leveled(level Level) Logger {
139139
l.level = level
140140
l.sinks = append([]Sink(nil), l.sinks...)
141-
for i, s := range l.sinks {
142-
if l2, ok := s.(Logger); ok {
143-
l.sinks[i] = l2.Leveled(level)
144-
}
145-
}
141+
return l
142+
}
143+
144+
// AppendSinks appends the sinks to the set sink
145+
// targets on the logger.
146+
func (l Logger) AppendSinks(s ...Sink) Logger {
147+
l.sinks = append(l.sinks, s...)
146148
return l
147149
}
148150

149151
func (l Logger) log(ctx context.Context, level Level, msg string, fields Map) {
150152
ent := l.entry(ctx, level, msg, fields)
151-
l.LogEntry(ctx, ent)
153+
l.Log(ctx, ent)
152154
}
153155

154156
func (l Logger) entry(ctx context.Context, level Level, msg string, fields Map) SinkEntry {

slog_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ func TestLogger(t *testing.T) {
3838

3939
s1 := &fakeSink{}
4040
s2 := &fakeSink{}
41-
l := slog.Make(s1, s2)
41+
l := slog.Make(s1)
4242
l = l.Leveled(slog.LevelError)
43+
l = l.AppendSinks(s2)
4344

4445
l.Info(bg, "wow", slog.Error(io.EOF))
4546
l.Error(bg, "meow", slog.Error(io.ErrUnexpectedEOF))
@@ -74,7 +75,7 @@ func TestLogger(t *testing.T) {
7475

7576
File: slogTestFile,
7677
Func: "cdr.dev/slog_test.TestLogger.func2",
77-
Line: 66,
78+
Line: 67,
7879

7980
Fields: slog.M(
8081
slog.F("ctx", 1024),
@@ -107,7 +108,7 @@ func TestLogger(t *testing.T) {
107108

108109
File: slogTestFile,
109110
Func: "cdr.dev/slog_test.TestLogger.func3",
110-
Line: 97,
111+
Line: 98,
111112

112113
SpanContext: span.SpanContext(),
113114

sloggers/sloghuman/sloghuman.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ import (
1212
"cdr.dev/slog/internal/syncwriter"
1313
)
1414

15-
// Make creates a logger that writes logs in a human
15+
// Sink creates a slog.Sink that writes logs in a human
1616
// readable YAML like format to the given writer.
1717
//
1818
// If the writer implements Sync() error then
1919
// it will be called when syncing.
20-
func Make(w io.Writer) slog.Logger {
21-
return slog.Make(&humanSink{
20+
func Sink(w io.Writer) slog.Sink {
21+
return &humanSink{
2222
w: syncwriter.New(w),
2323
w2: w,
24-
})
24+
}
2525
}
2626

2727
type humanSink struct {

sloggers/sloghuman/sloghuman_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestMake(t *testing.T) {
1717
t.Parallel()
1818

1919
b := &bytes.Buffer{}
20-
l := sloghuman.Make(b)
20+
l := slog.Make(sloghuman.Sink(b))
2121
l.Info(bg, "line1\n\nline2", slog.F("wowow", "me\nyou"))
2222
l.Sync()
2323

sloggers/slogjson/slogjson.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ import (
2929
"cdr.dev/slog/internal/syncwriter"
3030
)
3131

32-
// Make creates a logger that writes JSON logs
32+
// Sink creates a slog.Sink that writes JSON logs
3333
// to the given writer. See package level docs
3434
// for the format.
3535
// If the writer implements Sync() error then
3636
// it will be called when syncing.
37-
func Make(w io.Writer) slog.Logger {
37+
func Sink(w io.Writer) slog.Logger {
3838
return slog.Make(jsonSink{
3939
w: syncwriter.New(w),
4040
})

sloggers/slogjson/slogjson_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestMake(t *testing.T) {
2424

2525
ctx, s := trace.StartSpan(bg, "meow")
2626
b := &bytes.Buffer{}
27-
l := slogjson.Make(b)
27+
l := slogjson.Sink(b)
2828
l = l.Named("named")
2929
l.Error(ctx, "line1\n\nline2", slog.F("wowow", "me\nyou"))
3030

sloggers/slogstackdriver/slogstackdriver.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ import (
1717
"cdr.dev/slog/internal/syncwriter"
1818
)
1919

20-
// Make creates a slog.Logger configured to write JSON logs
20+
// Sink creates a slog.Sink configured to write JSON logs
2121
// to stdout for stackdriver.
2222
//
2323
// See https://cloud.google.com/logging/docs/agent
24-
func Make(w io.Writer) slog.Logger {
24+
func Sink(w io.Writer) slog.Sink {
2525
projectID, _ := metadata.ProjectID()
2626

27-
return slog.Make(stackdriverSink{
27+
return stackdriverSink{
2828
projectID: projectID,
2929
w: syncwriter.New(w),
30-
})
30+
}
3131
}
3232

3333
type stackdriverSink struct {

sloggers/slogstackdriver/slogstackdriver_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestStackdriver(t *testing.T) {
2525

2626
ctx, s := trace.StartSpan(bg, "meow")
2727
b := &bytes.Buffer{}
28-
l := slogstackdriver.Make(b)
28+
l := slog.Make(slogstackdriver.Sink(b))
2929
l = l.Named("meow")
3030
l.Error(ctx, "line1\n\nline2", slog.F("wowow", "me\nyou"))
3131

sloggers/slogtest/t.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818

1919
// Ensure all stdlib logs go through slog.
2020
func init() {
21-
l := sloghuman.Make(os.Stderr)
21+
l := slog.Make(sloghuman.Sink(os.Stderr))
2222
log.SetOutput(slog.Stdlib(context.Background(), l).Writer())
2323
}
2424

sloggers/slogtest/t_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"testing"
66

7-
"cdr.dev/slog"
87
"cdr.dev/slog/internal/assert"
98
"cdr.dev/slog/sloggers/slogtest"
109
)
@@ -31,9 +30,9 @@ func TestIgnoreErrors(t *testing.T) {
3130
t.Parallel()
3231

3332
tb := &fakeTB{}
34-
l := slog.Make(slogtest.Make(tb, &slogtest.Options{
33+
l := slogtest.Make(tb, &slogtest.Options{
3534
IgnoreErrors: true,
36-
}))
35+
})
3736

3837
l.Error(bg, "hello")
3938
assert.Equal(t, "errors", 0, tb.errors)

0 commit comments

Comments
 (0)