Skip to content

Commit 578097d

Browse files
authored
Merge pull request #63 from cdr/examples
Add more examples
2 parents f576970 + db2f860 commit 578097d

File tree

19 files changed

+170
-73
lines changed

19 files changed

+170
-73
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ jobs:
66
runs-on: ubuntu-latest
77
steps:
88
- uses: actions/checkout@v1
9-
- uses: actions/cache@v1
10-
with:
11-
path: ~/go/pkg/mod
12-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
13-
restore-keys: |
14-
${{ runner.os }}-go-
159
- name: make fmt
1610
uses: ./ci/image
1711
with:
@@ -21,12 +15,6 @@ jobs:
2115
runs-on: ubuntu-latest
2216
steps:
2317
- uses: actions/checkout@v1
24-
- uses: actions/cache@v1
25-
with:
26-
path: ~/go/pkg/mod
27-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
28-
restore-keys: |
29-
${{ runner.os }}-go-
3018
- name: make lint
3119
uses: ./ci/image
3220
with:
@@ -36,12 +24,6 @@ jobs:
3624
runs-on: ubuntu-latest
3725
steps:
3826
- uses: actions/checkout@v1
39-
- uses: actions/cache@v1
40-
with:
41-
path: ~/go/pkg/mod
42-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
43-
restore-keys: |
44-
${{ runner.os }}-go-
4527
- name: make test
4628
uses: ./ci/image
4729
with:
File renamed without changes.

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: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package slog_test
22

33
import (
4+
"context"
45
"io"
6+
"net"
7+
"os"
58
"testing"
69

710
"golang.org/x/xerrors"
811

912
"cdr.dev/slog"
13+
"cdr.dev/slog/sloggers/sloghuman"
14+
"cdr.dev/slog/sloggers/slogstackdriver"
1015
"cdr.dev/slog/sloggers/slogtest"
1116
)
1217

13-
func Example_slogtest() {
18+
func Example() {
1419
// Would be provided by the testing framework.
1520
var t testing.TB
1621

@@ -38,20 +43,61 @@ func Example_slogtest() {
3843
// - EOF
3944
}
4045

41-
func TestExample(t *testing.T) {
42-
t.Parallel()
46+
func ExampleWith() {
47+
ctx := slog.With(context.Background(), slog.F("field", 1))
4348

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-
)
49+
l := sloghuman.Make(os.Stdout)
50+
l.Info(ctx, "msg")
51+
52+
// 2019-12-07 20:54:23.986 [INFO] <example_test.go:20> msg {"field": 1}
53+
}
54+
55+
func ExampleStdlib() {
56+
ctx := slog.With(context.Background(), slog.F("field", 1))
57+
l := slog.Stdlib(ctx, sloghuman.Make(os.Stdout))
58+
59+
l.Print("msg")
60+
61+
// 2019-12-07 20:54:23.986 [INFO] (stdlib) <example_test.go:29> msg {"field": 1}
62+
}
63+
64+
func ExampleTee() {
65+
ctx := context.Background()
66+
l := sloghuman.Make(os.Stdout)
67+
68+
f, err := os.OpenFile("stackdriver", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
69+
if err != nil {
70+
l.Fatal(ctx, "failed to open stackdriver log file", slog.Error(err))
71+
}
72+
73+
l = slog.Tee(l, slogstackdriver.Make(f, nil))
74+
75+
l.Info(ctx, "log to stdout and stackdriver")
76+
77+
// 2019-12-07 20:59:55.790 [INFO] <example_test.go:46> log to stdout and stackdriver
78+
}
79+
80+
func ExampleLogger_Named() {
81+
ctx := context.Background()
82+
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)))
86+
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
57103
}

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+
}

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

internal/slogfmt/humanfmt.go renamed to internal/entryhuman/entry.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
// Package slogfmt contains the code to format slog.SinkEntry.
2-
package slogfmt
1+
// Package entryhuman contains the code to format slog.SinkEntry
2+
// for humans.
3+
package entryhuman
34

45
import (
56
"bytes"
@@ -8,7 +9,6 @@ import (
89
"io"
910
"os"
1011
"path/filepath"
11-
"regexp"
1212
"strconv"
1313
"strings"
1414
"time"
@@ -21,11 +21,6 @@ import (
2121
"cdr.dev/slog"
2222
)
2323

24-
// FilterJSONField filters the json field f from j.
25-
func FilterJSONField(j, f string) string {
26-
return regexp.MustCompile(`"`+f+`":[^,]+,`).ReplaceAllString(j, "")
27-
}
28-
2924
// StripTimestamp strips the timestamp from entry and returns
3025
// it and the rest of the entry.
3126
func StripTimestamp(ent string) (time.Time, string, error) {
@@ -47,14 +42,14 @@ func c(w io.Writer, attrs ...color.Attribute) *color.Color {
4742
return c
4843
}
4944

50-
// HumanEntry returns a human readable format for ent.
45+
// Fmt returns a human readable format for ent.
5146
//
5247
// We never return with a trailing newline because Go's testing framework adds one
5348
// automatically and if we include one, then we'll get two newlines.
5449
// We also do not indent the fields as go's test does that automatically
5550
// for extra lines in a log so if we did it here, the fields would be indented
5651
// twice in test logs. So the Stderr logger indents all the fields itself.
57-
func HumanEntry(w io.Writer, ent slog.SinkEntry) string {
52+
func Fmt(w io.Writer, ent slog.SinkEntry) string {
5853
var ents string
5954
ts := ent.Time.Format(TimeFormat)
6055
ents += ts + " "

internal/slogfmt/humanfmt_test.go renamed to internal/entryhuman/entry_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package slogfmt_test
1+
package entryhuman_test
22

33
import (
44
"io/ioutil"
@@ -9,7 +9,7 @@ import (
99

1010
"cdr.dev/slog"
1111
"cdr.dev/slog/internal/assert"
12-
"cdr.dev/slog/internal/slogfmt"
12+
"cdr.dev/slog/internal/entryhuman"
1313
)
1414

1515
var kt = time.Date(2000, time.February, 5, 4, 4, 4, 4, time.UTC)
@@ -18,7 +18,7 @@ func TestEntry(t *testing.T) {
1818
t.Parallel()
1919

2020
test := func(t *testing.T, in slog.SinkEntry, exp string) {
21-
act := slogfmt.HumanEntry(ioutil.Discard, in)
21+
act := entryhuman.Fmt(ioutil.Discard, in)
2222
assert.Equal(t, exp, act, "entry")
2323
}
2424

@@ -71,7 +71,7 @@ line2`)
7171
t.Run("color", func(t *testing.T) {
7272
t.Parallel()
7373

74-
act := slogfmt.HumanEntry(slogfmt.ForceColorWriter, slog.SinkEntry{
74+
act := entryhuman.Fmt(entryhuman.ForceColorWriter, slog.SinkEntry{
7575
Level: slog.LevelCritical,
7676
Fields: slog.M(
7777
slog.F("hey", "hi"),
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
package slogfmt
1+
package entryhuman
22

33
var ForceColorWriter = forceColorWriter

internal/slogfmt/json.go renamed to internal/entryhuman/json.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package slogfmt
1+
package entryhuman
22

33
import (
44
"bytes"

0 commit comments

Comments
 (0)