Skip to content

Commit dd1f483

Browse files
committed
Add more examples
1 parent f576970 commit dd1f483

File tree

5 files changed

+75
-23
lines changed

5 files changed

+75
-23
lines changed
File renamed without changes.

example_test.go

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,54 @@
11
package slog_test
22

33
import (
4+
"context"
45
"io"
6+
"os"
57
"testing"
68

79
"golang.org/x/xerrors"
810

911
"cdr.dev/slog"
12+
"cdr.dev/slog/sloggers/sloghuman"
13+
"cdr.dev/slog/sloggers/slogstackdriver"
1014
"cdr.dev/slog/sloggers/slogtest"
1115
)
1216

13-
func Example_slogtest() {
17+
func ExampleWith() {
18+
ctx := slog.With(context.Background(), slog.F("field", 1))
19+
20+
l := sloghuman.Make(os.Stdout)
21+
l.Info(ctx, "msg")
22+
23+
// 2019-12-07 20:54:23.986 [INFO] <example_test.go:20> msg {"field": 1}
24+
}
25+
26+
func ExampleStdlib() {
27+
ctx := slog.With(context.Background(), slog.F("field", 1))
28+
l := slog.Stdlib(ctx, sloghuman.Make(os.Stdout))
29+
30+
l.Print("msg")
31+
32+
// 2019-12-07 20:54:23.986 [INFO] (stdlib) <example_test.go:29> msg {"field": 1}
33+
}
34+
35+
func ExampleTee() {
36+
ctx := context.Background()
37+
l := sloghuman.Make(os.Stdout)
38+
39+
f, err := os.OpenFile("stackdriver", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
40+
if err != nil {
41+
l.Fatal(ctx, "failed to open stackdriver log file", slog.Error(err))
42+
}
43+
44+
l = slog.Tee(l, slogstackdriver.Make(f, nil))
45+
46+
l.Info(ctx, "log to stdout and stackdriver")
47+
48+
// 2019-12-07 20:59:55.790 [INFO] <example_test.go:46> log to stdout and stackdriver
49+
}
50+
51+
func Example() {
1452
// Would be provided by the testing framework.
1553
var t testing.TB
1654

@@ -37,21 +75,3 @@ func Example_slogtest() {
3775
// /Users/nhooyr/src/cdr/slog/examples_test.go:49
3876
// - EOF
3977
}
40-
41-
func TestExample(t *testing.T) {
42-
t.Parallel()
43-
44-
slogtest.Info(t, "my message here",
45-
slog.F("field_name", "something or the other"),
46-
slog.F("some_map", slog.M(
47-
slog.F("nested_fields", "wowow"),
48-
)),
49-
slog.Error(
50-
xerrors.Errorf("wrap1: %w",
51-
xerrors.Errorf("wrap2: %w",
52-
io.EOF,
53-
),
54-
),
55-
),
56-
)
57-
}

example_value_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package slog_test
2+
3+
import (
4+
"context"
5+
"os"
6+
7+
"cdr.dev/slog"
8+
"cdr.dev/slog/sloggers/sloghuman"
9+
)
10+
11+
type vals struct {
12+
first int
13+
second int
14+
}
15+
16+
func (s *vals) SlogValue() interface{} {
17+
return slog.M(
18+
slog.F("total", s.first+s.second),
19+
slog.F("first", s.first),
20+
slog.F("second", s.second),
21+
)
22+
}
23+
24+
func ExampleValue() {
25+
l := sloghuman.Make(os.Stdout)
26+
l.Info(context.Background(), "hello", slog.F("val", &vals{
27+
first: 3,
28+
second: 6,
29+
}))
30+
31+
// 2019-12-07 21:06:14.636 [INFO] <example_value_test.go:26> hello {"val": {"total": 9, "first": 3, "second": 6}}
32+
}

slog.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ func fieldsFromContext(ctx context.Context) Map {
5656
return l
5757
}
5858

59-
// Context returns a context that contains the given fields.
59+
// With returns a context that contains the given fields.
6060
// Any logs written with the provided context will have
6161
// the given logs prepended.
6262
// It will append to any fields already in ctx.
63-
func Context(ctx context.Context, fields ...Field) context.Context {
63+
func With(ctx context.Context, fields ...Field) context.Context {
6464
f1 := fieldsFromContext(ctx)
6565
f2 := combineFields(f1, fields)
6666
return fieldsWithContext(ctx, f2)

slog_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func TestLogger(t *testing.T) {
6565
l.Debug(ctx, "logging in helper")
6666
}
6767

68-
ctx := slog.Context(bg, slog.F(
68+
ctx := slog.With(bg, slog.F(
6969
"ctx", 1024),
7070
)
7171
h(ctx)
@@ -96,7 +96,7 @@ func TestLogger(t *testing.T) {
9696
l = l.Named("hello2")
9797

9898
ctx, span := trace.StartSpan(bg, "trace")
99-
ctx = slog.Context(ctx, slog.F("ctx", io.EOF))
99+
ctx = slog.With(ctx, slog.F("ctx", io.EOF))
100100
l = l.With(slog.F("with", 2))
101101

102102
l.Info(ctx, "meow", slog.F("hi", "xd"))

0 commit comments

Comments
 (0)