Skip to content

Commit b1eadde

Browse files
committed
Revert "Merge pull request #85 from cdr/ctx-log"
This reverts commit 95bc0f1, reversing changes made to f7a90d8.
1 parent 958f7eb commit b1eadde

21 files changed

+150
-230
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ go get cdr.dev/slog
3535
Many more examples available at [godoc](https://godoc.org/cdr.dev/slog#pkg-examples).
3636

3737
```go
38-
ctx := sloghuman.Make(ctx, os.Stdout)
38+
log := sloghuman.Make(os.Stdout)
3939

40-
slog.Info(ctx, "my message here",
40+
log.Info(context.Background(), "my message here",
4141
slog.F("field_name", "something or the other"),
4242
slog.F("some_map", slog.M(
4343
slog.F("nested_fields", time.Date(2000, time.February, 5, 4, 4, 4, 0, time.UTC)),
@@ -87,8 +87,6 @@ Here is a list of reasons how we improved on zap with slog.
8787

8888
1. Full [context.Context](https://blog.golang.org/context) support
8989
- `slog` lets you set fields in a `context.Context` such that any log with the context prints those fields.
90-
- `slog` stores the actual logger in the `context.Context`, following the example of
91-
[the Go trace library](https://golang.org/pkg/runtime/trace/). Our logger doesn't bloat type and function signatures.
9290
- We wanted to be able to pull up all relevant logs for a given trace, user or request. With zap, we were plugging
9391
these fields in for every relevant log or passing around a logger with the fields set. This became very verbose.
9492

context.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

example_helper_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import (
1212
func httpLogHelper(ctx context.Context, status int) {
1313
slog.Helper()
1414

15-
slog.Info(ctx, "sending HTTP response",
15+
l.Info(ctx, "sending HTTP response",
1616
slog.F("status", status),
1717
)
1818
}
1919

20-
var l = sloghuman.Make(context.Background(), os.Stdout)
20+
var l = sloghuman.Make(os.Stdout)
2121

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

example_marshaller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ func (s myStruct) MarshalJSON() ([]byte, error) {
2121
}
2222

2323
func Example_marshaller() {
24-
ctx := sloghuman.Make(context.Background(), os.Stdout)
24+
l := sloghuman.Make(os.Stdout)
2525

26-
slog.Info(ctx, "wow",
26+
l.Info(context.Background(), "wow",
2727
slog.F("myStruct", myStruct{
2828
foo: 1,
2929
bar: 2,

example_test.go

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ import (
1818
)
1919

2020
func Example() {
21-
ctx := sloghuman.Make(context.Background(), os.Stdout)
21+
log := sloghuman.Make(os.Stdout)
2222

23-
slog.Info(ctx, "my message here",
23+
log.Info(context.Background(), "my message here",
2424
slog.F("field_name", "something or the other"),
2525
slog.F("some_map", slog.M(
2626
slog.F("nested_fields", time.Date(2000, time.February, 5, 4, 4, 4, 0, time.UTC)),
2727
)),
28-
slog.Err(
28+
slog.Error(
2929
xerrors.Errorf("wrap1: %w",
3030
xerrors.Errorf("wrap2: %w",
3131
io.EOF,
@@ -45,15 +45,15 @@ func Example() {
4545
}
4646

4747
func Example_struct() {
48-
ctx := sloghuman.Make(context.Background(), os.Stdout)
48+
l := sloghuman.Make(os.Stdout)
4949

5050
type hello struct {
5151
Meow int `json:"meow"`
5252
Bar string `json:"bar"`
5353
M time.Time `json:"m"`
5454
}
5555

56-
slog.Info(ctx, "check out my structure",
56+
l.Info(context.Background(), "check out my structure",
5757
slog.F("hello", hello{
5858
Meow: 1,
5959
Bar: "barbar",
@@ -76,69 +76,68 @@ func Example_testing() {
7676
}
7777

7878
func Example_tracing() {
79-
var ctx context.Context
80-
ctx = sloghuman.Make(context.Background(), os.Stdout)
79+
log := sloghuman.Make(os.Stdout)
8180

82-
ctx, _ = trace.StartSpan(ctx, "spanName")
81+
ctx, _ := trace.StartSpan(context.Background(), "spanName")
8382

84-
slog.Info(ctx, "my msg", slog.F("hello", "hi"))
83+
log.Info(ctx, "my msg", slog.F("hello", "hi"))
8584

8685
// 2019-12-09 21:59:48.110 [INFO] <example_test.go:62> my msg {"trace": "f143d018d00de835688453d8dc55c9fd", "span": "f214167bf550afc3", "hello": "hi"}
8786
}
8887

8988
func Example_multiple() {
90-
ctx := sloghuman.Make(context.Background(), os.Stdout)
89+
l := sloghuman.Make(os.Stdout)
9190

9291
f, err := os.OpenFile("stackdriver", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
9392
if err != nil {
94-
slog.Fatal(ctx, "failed to open stackdriver log file", slog.Err(err))
93+
l.Fatal(context.Background(), "failed to open stackdriver log file", slog.Error(err))
9594
}
9695

97-
ctx = slog.Make(l, slogstackdriver.Make(ctx, f))
96+
l = slog.Make(l, slogstackdriver.Make(f))
9897

99-
slog.Info(ctx, "log to stdout and stackdriver")
98+
l.Info(context.Background(), "log to stdout and stackdriver")
10099

101100
// 2019-12-07 20:59:55.790 [INFO] <example_test.go:46> log to stdout and stackdriver
102101
}
103102

104103
func ExampleWith() {
105104
ctx := slog.With(context.Background(), slog.F("field", 1))
106105

107-
ctx = sloghuman.Make(ctx, os.Stdout)
108-
slog.Info(ctx, "msg")
106+
l := sloghuman.Make(os.Stdout)
107+
l.Info(ctx, "msg")
109108

110109
// 2019-12-07 20:54:23.986 [INFO] <example_test.go:20> msg {"field": 1}
111110
}
112111

113112
func ExampleStdlib() {
114113
ctx := slog.With(context.Background(), slog.F("field", 1))
115-
l := slog.Stdlib(sloghuman.Make(ctx, os.Stdout))
114+
l := slog.Stdlib(ctx, sloghuman.Make(os.Stdout))
116115

117116
l.Print("msg")
118117

119118
// 2019-12-07 20:54:23.986 [INFO] (stdlib) <example_test.go:29> msg {"field": 1}
120119
}
121120

122-
func ExampleNamed() {
121+
func ExampleLogger_Named() {
123122
ctx := context.Background()
124123

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

129128
// 2019-12-07 21:20:56.974 [INFO] (http) <example_test.go:85> received request {"remote address": "127.0.0.1"}
130129
}
131130

132-
func ExampleLeveled() {
131+
func ExampleLogger_Leveled() {
133132
ctx := context.Background()
134133

135-
ctx = sloghuman.Make(ctx, os.Stdout)
136-
slog.Debug(ctx, "testing1")
137-
slog.Info(ctx, "received request")
134+
l := sloghuman.Make(os.Stdout)
135+
l.Debug(ctx, "testing1")
136+
l.Info(ctx, "received request")
138137

139-
ctx = slog.Leveled(ctx, slog.LevelDebug)
138+
l = l.Leveled(slog.LevelDebug)
140139

141-
slog.Debug(ctx, "testing2")
140+
l.Debug(ctx, "testing2")
142141

143142
// 2019-12-07 21:26:20.945 [INFO] <example_test.go:95> received request
144143
// 2019-12-07 21:26:20.945 [DEBUG] <example_test.go:99> testing2

export_test.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
package slog
22

3-
import "context"
4-
5-
func SetExit(ctx context.Context, fn func(int)) context.Context {
6-
l, ok := loggerFromContext(ctx)
7-
if !ok {
8-
return ctx
9-
}
3+
func (l *Logger) SetExit(fn func(int)) {
104
l.exit = fn
11-
return contextWithLogger(ctx, l)
125
}

map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func encodeJSON(v interface{}) []byte {
128128
b, err := json.Marshal(v)
129129
if err != nil {
130130
return encode(M(
131-
Err(xerrors.Errorf("failed to marshal to JSON: %w", err)),
131+
Error(xerrors.Errorf("failed to marshal to JSON: %w", err)),
132132
F("type", reflect.TypeOf(v)),
133133
F("value", fmt.Sprintf("%+v", v)),
134134
))

map_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestMap(t *testing.T) {
3737
}
3838

3939
test(t, slog.M(
40-
slog.Err(
40+
slog.Error(
4141
xerrors.Errorf("wrap1: %w",
4242
xerrors.Errorf("wrap2: %w",
4343
io.EOF,
@@ -222,6 +222,10 @@ func TestMap(t *testing.T) {
222222
})
223223
}
224224

225+
type meow struct {
226+
a int
227+
}
228+
225229
func indentJSON(t *testing.T, j string) string {
226230
b := &bytes.Buffer{}
227231
err := json.Indent(b, []byte(j), "", strings.Repeat(" ", 4))

s.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package slog
33
import (
44
"context"
55
"log"
6-
"os"
76
"strings"
87
)
98

@@ -16,26 +15,22 @@ import (
1615
// You can redirect the stdlib default logger with log.SetOutput
1716
// to the Writer on the logger returned by this function.
1817
// See the example.
19-
func Stdlib(ctx context.Context) *log.Logger {
20-
ctx = Named(ctx, "stdlib")
21-
22-
l, ok := loggerFromContext(ctx)
23-
if !ok {
24-
// Give stderr logger if no slog.
25-
return log.New(os.Stderr, "", 0)
26-
}
18+
func Stdlib(ctx context.Context, l Logger) *log.Logger {
2719
l.skip += 3
28-
ctx = contextWithLogger(ctx, l)
20+
21+
l = l.Named("stdlib")
2922

3023
w := &stdlogWriter{
3124
ctx: ctx,
25+
l: l,
3226
}
3327

3428
return log.New(w, "", 0)
3529
}
3630

3731
type stdlogWriter struct {
3832
ctx context.Context
33+
l Logger
3934
}
4035

4136
func (w stdlogWriter) Write(p []byte) (n int, err error) {
@@ -44,7 +39,7 @@ func (w stdlogWriter) Write(p []byte) (n int, err error) {
4439
// we do not want.
4540
msg = strings.TrimSuffix(msg, "\n")
4641

47-
Info(w.ctx, msg)
42+
w.l.Info(w.ctx, msg)
4843

4944
return len(p), nil
5045
}

s_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package slog_test
22

33
import (
44
"bytes"
5-
"context"
65
"testing"
76

87
"cdr.dev/slog"
@@ -15,16 +14,14 @@ func TestStdlib(t *testing.T) {
1514
t.Parallel()
1615

1716
b := &bytes.Buffer{}
18-
ctx := context.Background()
19-
ctx = slog.Make(sloghuman.Make(ctx, b))
20-
ctx = slog.With(ctx,
17+
l := slog.Make(sloghuman.Make(b)).With(
2118
slog.F("hi", "we"),
2219
)
23-
stdlibLog := slog.Stdlib(ctx)
20+
stdlibLog := slog.Stdlib(bg, l)
2421
stdlibLog.Println("stdlib")
2522

2623
et, rest, err := entryhuman.StripTimestamp(b.String())
2724
assert.Success(t, "strip timestamp", err)
2825
assert.False(t, "timestamp", et.IsZero())
29-
assert.Equal(t, "entry", " [INFO]\t(stdlib)\t<s_test.go:24>\tstdlib\t{\"hi\": \"we\"}\n", rest)
26+
assert.Equal(t, "entry", " [INFO]\t(stdlib)\t<s_test.go:21>\tstdlib\t{\"hi\": \"we\"}\n", rest)
3027
}

0 commit comments

Comments
 (0)