Skip to content

Commit 7b617dc

Browse files
committed
Add more examples
1 parent ef50d40 commit 7b617dc

File tree

5 files changed

+88
-31
lines changed

5 files changed

+88
-31
lines changed

example_helper_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package slog_test
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"os"
7+
8+
"cdr.dev/slog"
9+
"cdr.dev/slog/sloggers/sloghuman"
10+
)
11+
12+
func httpLogHelper(ctx context.Context, status int) {
13+
slog.Helper()
14+
15+
l.Info(ctx, "sending HTTP response",
16+
slog.F("status", status),
17+
)
18+
}
19+
20+
var l = sloghuman.Make(os.Stdout)
21+
22+
func ExampleHelper() {
23+
ctx := context.Background()
24+
httpLogHelper(ctx, http.StatusBadGateway)
25+
26+
// 2019-12-07 21:18:42.567 [INFO] <example_helper_test.go:24> sending HTTP response {"status": 502}
27+
}

example_test.go

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package slog_test
33
import (
44
"context"
55
"io"
6+
"net"
67
"os"
78
"testing"
89

@@ -14,6 +15,34 @@ import (
1415
"cdr.dev/slog/sloggers/slogtest"
1516
)
1617

18+
func Example() {
19+
// Would be provided by the testing framework.
20+
var t testing.TB
21+
22+
slogtest.Info(t, "my message here",
23+
slog.F("field_name", "something or the other"),
24+
slog.F("some_map", slog.M(
25+
slog.F("nested_fields", "wowow"),
26+
)),
27+
slog.Error(
28+
xerrors.Errorf("wrap1: %w",
29+
xerrors.Errorf("wrap2: %w",
30+
io.EOF,
31+
),
32+
),
33+
),
34+
)
35+
36+
// t.go:55: 2019-12-05 21:20:31.218 [INFO] <examples_test.go:42> my message here {"field_name": "something or the other", "some_map": {"nested_fields": "wowow"}} ...
37+
// "error": wrap1:
38+
// cdr.dev/slog_test.TestExample
39+
// /Users/nhooyr/src/cdr/slog/examples_test.go:48
40+
// - wrap2:
41+
// cdr.dev/slog_test.TestExample
42+
// /Users/nhooyr/src/cdr/slog/examples_test.go:49
43+
// - EOF
44+
}
45+
1746
func ExampleWith() {
1847
ctx := slog.With(context.Background(), slog.F("field", 1))
1948

@@ -48,30 +77,27 @@ func ExampleTee() {
4877
// 2019-12-07 20:59:55.790 [INFO] <example_test.go:46> log to stdout and stackdriver
4978
}
5079

51-
func Example() {
52-
// Would be provided by the testing framework.
53-
var t testing.TB
80+
func ExampleLogger_Named() {
81+
ctx := context.Background()
5482

55-
slogtest.Info(t, "my message here",
56-
slog.F("field_name", "something or the other"),
57-
slog.F("some_map", slog.M(
58-
slog.F("nested_fields", "wowow"),
59-
)),
60-
slog.Error(
61-
xerrors.Errorf("wrap1: %w",
62-
xerrors.Errorf("wrap2: %w",
63-
io.EOF,
64-
),
65-
),
66-
),
67-
)
83+
l := sloghuman.Make(os.Stdout)
84+
l = l.Named("http")
85+
l.Info(ctx, "received request", slog.F("remote address", net.IPv4(127, 0, 0, 1)))
6886

69-
// t.go:55: 2019-12-05 21:20:31.218 [INFO] <examples_test.go:42> my message here {"field_name": "something or the other", "some_map": {"nested_fields": "wowow"}} ...
70-
// "error": wrap1:
71-
// cdr.dev/slog_test.TestExample
72-
// /Users/nhooyr/src/cdr/slog/examples_test.go:48
73-
// - wrap2:
74-
// cdr.dev/slog_test.TestExample
75-
// /Users/nhooyr/src/cdr/slog/examples_test.go:49
76-
// - EOF
87+
// 2019-12-07 21:20:56.974 [INFO] (http) <example_test.go:85> received request {"remote address": "127.0.0.1"}
88+
}
89+
90+
func ExampleLogger_SetLevel() {
91+
ctx := context.Background()
92+
93+
l := sloghuman.Make(os.Stdout)
94+
l.Debug(ctx, "testing1")
95+
l.Info(ctx, "received request")
96+
97+
l.SetLevel(slog.LevelDebug)
98+
99+
l.Debug(ctx, "testing2")
100+
101+
// 2019-12-07 21:26:20.945 [INFO] <example_test.go:95> received request
102+
// 2019-12-07 21:26:20.945 [DEBUG] <example_test.go:99> testing2
77103
}

internal/assert/assert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func Len(t testing.TB, n int, a interface{}, name string) {
6767
t.Helper()
6868
act := reflect.ValueOf(a).Len()
6969
if n != act {
70-
t.Fatalf("unexpected length %v of %v: %v", act, name, act)
70+
t.Fatalf("expected len(%v) == %v but got %v", name, n, act)
7171
}
7272
}
7373

slog.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Package slog implements minimal structured logging.
22
//
3-
// See https://cdr.dev/slog for more overview docs and a comparison with existing libraries.
3+
// See https://cdr.dev/slog for overview docs and a comparison with existing libraries.
44
//
5-
// Sink implementations available in the sloggers subpackage.
5+
// The examples are the best way to understand how to use this library effectively.
6+
//
7+
// Sink implementations are in the sloggers subdirectory.
68
package slog // import "cdr.dev/slog"
79

810
import (
@@ -121,7 +123,8 @@ type Sink interface {
121123
Sync() error
122124
}
123125

124-
// Make creates a logger that writes logs to sink.
126+
// Make creates a logger that writes logs to sink
127+
// at LevelInfo.
125128
func Make(s Sink) Logger {
126129
var l Logger
127130
l.sinks = []sink{
@@ -130,7 +133,7 @@ func Make(s Sink) Logger {
130133
level: new(int64),
131134
},
132135
}
133-
l.SetLevel(LevelDebug)
136+
l.SetLevel(LevelInfo)
134137
return l
135138
}
136139

slog_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestLogger(t *testing.T) {
6262
l := slog.Make(s)
6363
h := func(ctx context.Context) {
6464
slog.Helper()
65-
l.Debug(ctx, "logging in helper")
65+
l.Info(ctx, "logging in helper")
6666
}
6767

6868
ctx := slog.With(bg, slog.F(
@@ -74,7 +74,7 @@ func TestLogger(t *testing.T) {
7474
assert.Equal(t, slog.SinkEntry{
7575
Time: s.entries[0].Time,
7676

77-
Level: slog.LevelDebug,
77+
Level: slog.LevelInfo,
7878
Message: "logging in helper",
7979

8080
File: slogTestFile,
@@ -129,6 +129,7 @@ func TestLogger(t *testing.T) {
129129

130130
s := &fakeSink{}
131131
l := slog.Make(s)
132+
l.SetLevel(slog.LevelDebug)
132133

133134
l.Debug(bg, "")
134135
l.Info(bg, "")

0 commit comments

Comments
 (0)