Skip to content

Commit a2b34ad

Browse files
committed
Improve docs
1 parent 6c5d3fe commit a2b34ad

File tree

10 files changed

+105
-92
lines changed

10 files changed

+105
-92
lines changed

README.md

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,43 +20,37 @@ go get go.coder.com/slog
2020
- First class [context.Context](https://blog.golang.org/context) support
2121
- Beautiful logging output by default
2222
- Multiple adapters
23-
- First class [\*testing.T](https://godoc.org/go.coder.com/slog/slogtest) support
23+
- First class [\testing.TB](https://godoc.org/go.coder.com/slog/slogtest) support
2424

2525
## Example
2626

2727
```go
28-
testlog.Info(t, "my message here",
28+
slogtest.Info(t, "my message here",
2929
slog.F("field_name", "something or the other"),
30-
slog.F("some_map", map[string]interface{}{
31-
"nested_fields": "wowow",
32-
}),
33-
slog.F("some slice", []interface{}{
34-
1,
35-
"foof",
36-
"bar",
37-
true,
38-
}),
30+
slog.F("some_map", slog.Map(
31+
slog.F("nested_fields", "wowow"),
32+
)),
33+
slog.Error(
34+
xerrors.Errorf("wrap1: %w",
35+
xerrors.Errorf("wrap2: %w",
36+
io.EOF),
37+
)),
3938
slog.Component("test"),
40-
41-
slog.F("name", slog.ValueFunc(func() interface{} {
42-
return "wow"
43-
})),
4439
)
4540

46-
// --- PASS: TestExampleTest (0.00s)
47-
// test_test.go:38: Sep 06 14:33:52.628 [INFO] (test): my_message_here
41+
// --- PASS: TestExample (0.00s)
42+
// examples_test.go:46: Sep 08 13:54:34.532 [INFO] (test): my_message_here
4843
// field_name: something or the other
4944
// some_map:
5045
// nested_fields: wowow
5146
// error:
52-
// - msg: wrap2
53-
// loc: /Users/nhooyr/src/cdr/slog/test_test.go:43
54-
// fun: go.coder.com/slog_test.TestExampleTest
55-
// - msg: wrap1
56-
// loc: /Users/nhooyr/src/cdr/slog/test_test.go:44
57-
// fun: go.coder.com/slog_test.TestExampleTest
47+
// - wrap1
48+
// go.coder.com/slog_test.TestExample
49+
// /Users/nhooyr/src/cdr/slog/examples_test.go:52
50+
// - wrap2
51+
// go.coder.com/slog_test.TestExample
52+
// /Users/nhooyr/src/cdr/slog/examples_test.go:53
5853
// - EOF
59-
// name: wow
6054
```
6155

6256
## Design justifications

ci/lint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ cd "$(git rev-parse --show-toplevel)"
77
# shellcheck disable=SC2046
88
shellcheck -x $(git ls-files "*.sh")
99
go vet ./...
10-
#go run golang.org/x/lint/golint -set_exit_status ./...
10+
go run golang.org/x/lint/golint -set_exit_status ./...

examples_test.go

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,57 @@
11
package slog_test
22

33
import (
4+
"golang.org/x/xerrors"
5+
"io"
46
"testing"
57

68
"go.coder.com/slog"
79
"go.coder.com/slog/sloggers/slogtest"
810
)
911

10-
func Example_test() {
12+
func Example() {
1113
// Nil here but would be provided by the testing framework.
12-
var t *testing.T
14+
var t testing.TB
1315

1416
slogtest.Info(t, "my message here",
1517
slog.F("field_name", "something or the other"),
16-
slog.F("some_map", map[string]interface{}{
17-
"nested_fields": "wowow",
18-
}),
19-
slog.F("some slice", []interface{}{
20-
1,
21-
"foof",
22-
"bar",
23-
true,
24-
}),
18+
slog.F("some_map", slog.Map(
19+
slog.F("nested_fields", "wowow"),
20+
)),
21+
slog.Error(
22+
xerrors.Errorf("wrap1: %w",
23+
xerrors.Errorf("wrap2: %w",
24+
io.EOF),
25+
)),
2526
slog.Component("test"),
26-
27-
slog.F("name", "hi"),
2827
)
2928

30-
// --- PASS: TestExampleTest (0.00s)
31-
// test_test.go:38: Sep 06 14:33:52.628 [INFO] (test): my_message_here
29+
// --- PASS: TestExample (0.00s)
30+
// examples_test.go:46: Sep 08 13:54:34.532 [INFO] (test): my_message_here
3231
// field_name: something or the other
3332
// some_map:
3433
// nested_fields: wowow
3534
// error:
36-
// - msg: wrap2
37-
// loc: /Users/nhooyr/src/cdr/slog/test_test.go:43
38-
// fun: go.coder.com/slog_test.TestExampleTest
39-
// - msg: wrap1
40-
// loc: /Users/nhooyr/src/cdr/slog/test_test.go:44
41-
// fun: go.coder.com/slog_test.TestExampleTest
35+
// - wrap1
36+
// go.coder.com/slog_test.TestExample
37+
// /Users/nhooyr/src/cdr/slog/examples_test.go:52
38+
// - wrap2
39+
// go.coder.com/slog_test.TestExample
40+
// /Users/nhooyr/src/cdr/slog/examples_test.go:53
4241
// - EOF
43-
// name: wow
42+
}
43+
44+
func TestExample(t *testing.T) {
45+
slogtest.Info(t, "my message here",
46+
slog.F("field_name", "something or the other"),
47+
slog.F("some_map", slog.Map(
48+
slog.F("nested_fields", "wowow"),
49+
)),
50+
slog.Error(
51+
xerrors.Errorf("wrap1: %w",
52+
xerrors.Errorf("wrap2: %w",
53+
io.EOF),
54+
)),
55+
slog.Component("test"),
56+
)
4457
}

internal/diff/diff.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Package diff is a helper package around go-cmp for generating
2+
// diffs between arbitrary interfaces.
3+
// See https://github.com/google/go-cmp/issues/40#issuecomment-328615283
4+
// Copied from https://github.com/nhooyr/websocket/blob/1b874731eab56c69c8bb3ebf8a029020c7863fc9/cmp_test.go
15
package diff
26

37
import (
@@ -6,8 +10,8 @@ import (
610
"github.com/google/go-cmp/cmp"
711
)
812

9-
// https://github.com/google/go-cmp/issues/40#issuecomment-328615283
10-
// Copied from https://github.com/nhooyr/websocket/blob/1b874731eab56c69c8bb3ebf8a029020c7863fc9/cmp_test.go
13+
// Diff returns a diff between exp and act.
14+
// The empty string is returned if they are identical.
1115
func Diff(exp, act interface{}) string {
1216
return cmp.Diff(exp, act, deepAllowUnexported(exp, act))
1317
}

internal/humanfmt/humanfmt.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Package humanfmt contains the code to format slog.Entry
2+
// into a human readable format.
13
package humanfmt
24

35
import (
@@ -13,6 +15,7 @@ import (
1315
"go.coder.com/slog/slogval"
1416
)
1517

18+
// Entry returns a human readable format for ent.
1619
func Entry(ent slog.Entry, enableColor bool) string {
1720
var ents string
1821
if ent.File != "" {

sloggers/sloghuman/sloghuman.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Package sloghuman contains the slogger
2+
// that writes logs in a human readable format.
13
package sloghuman // import "go.coder.com/slog/sloggers/sloghuman"
24

35
import (

sloggers/slogjson/slogjson.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Package slogjson contains the slogger
2+
// that writes logs in a JSON format.
13
package slogjson // import "go.coder.com/slog/sloggers/slogjson"
24

35
import (

sloggers/slogtest/global.go

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

sloggers/slogtest/test.go renamed to sloggers/slogtest/slogtest.go

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package slogtest
1+
// Package slogtest contains the slogger for use
2+
// with Go's testing package.
3+
package slogtest // import "go.coder.com/slog/sloggers/slogtest"
24

35
import (
46
"context"
@@ -17,7 +19,7 @@ type TestOptions struct {
1719
IgnoreErrors bool
1820
}
1921

20-
// Test creates a Logger that writes logs to tb.
22+
// Test creates a Logger that writes logs to tb in a human readable format.
2123
func Make(tb testing.TB, opts *TestOptions) slog.Logger {
2224
if opts == nil {
2325
opts = &TestOptions{}
@@ -28,6 +30,14 @@ func Make(tb testing.TB, opts *TestOptions) slog.Logger {
2830
})
2931
}
3032

33+
// testSink implements slog.Sink but also
34+
//
35+
// type testSink interface {
36+
// Stdlib() Sink
37+
// TestingHelper() func()
38+
// }
39+
//
40+
// See slog.go in root package.
3141
type testSink struct {
3242
tb testing.TB
3343
opts *TestOptions
@@ -39,12 +49,6 @@ func (ts testSink) Stdlib() slog.Sink {
3949
return ts
4050
}
4151

42-
// Implements:
43-
// type testSink interface {
44-
// Stdlib() Sink
45-
// TestingHelper() func()
46-
// }
47-
// See slog.go in root package.
4852
func (ts testSink) TestingHelper() func() {
4953
return ts.tb.Helper
5054
}
@@ -82,3 +86,29 @@ func (ts testSink) LogEntry(ctx context.Context, ent slog.Entry) {
8286
ts.tb.Fatal(s)
8387
}
8488
}
89+
90+
var ctx = context.Background()
91+
92+
// Debug logs the given msg and fields to t via t.Log at the debug level.
93+
func Debug(t testing.TB, msg string, fields ...slog.Field) {
94+
t.Helper()
95+
Make(t, nil).Debug(ctx, msg, fields...)
96+
}
97+
98+
// Info logs the given msg and fields to t via t.Log at the info level.
99+
func Info(t testing.TB, msg string, fields ...slog.Field) {
100+
t.Helper()
101+
Make(t, nil).Info(ctx, msg, fields...)
102+
}
103+
104+
// Error logs the given msg and fields to t via t.Error at the error level.
105+
func Error(t testing.TB, msg string, fields ...slog.Field) {
106+
t.Helper()
107+
Make(t, nil).Error(ctx, msg, fields...)
108+
}
109+
110+
// Fatal logs the given msg and fields to t via t.Fatal at the fatal level.
111+
func Fatal(t testing.TB, msg string, fields ...slog.Field) {
112+
t.Helper()
113+
Make(t, nil).Fatal(ctx, msg, fields...)
114+
}
File renamed without changes.

0 commit comments

Comments
 (0)