Skip to content

Commit f7a90d8

Browse files
committed
Improve docs and add more examples
1 parent daaf96c commit f7a90d8

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

example_marshaller_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 myStruct struct {
12+
foo int
13+
bar int
14+
}
15+
16+
func (s myStruct) MarshalJSON() ([]byte, error) {
17+
return slog.M(
18+
slog.F("foo", s.foo),
19+
slog.F("bar", s.foo),
20+
).MarshalJSON()
21+
}
22+
23+
func Example_marshaller() {
24+
l := sloghuman.Make(os.Stdout)
25+
26+
l.Info(context.Background(), "wow",
27+
slog.F("myStruct", myStruct{
28+
foo: 1,
29+
bar: 2,
30+
}),
31+
)
32+
33+
// 2019-12-16 17:31:37.120 [INFO] <example_marshaller_test.go:26> wow {"myStruct": {"foo": 1, "bar": 1}}
34+
}

example_test.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ func Example() {
4444
// - EOF
4545
}
4646

47+
func Example_struct() {
48+
l := sloghuman.Make(os.Stdout)
49+
50+
type hello struct {
51+
Meow int `json:"meow"`
52+
Bar string `json:"bar"`
53+
M time.Time `json:"m"`
54+
}
55+
56+
l.Info(context.Background(), "check out my structure",
57+
slog.F("hello", hello{
58+
Meow: 1,
59+
Bar: "barbar",
60+
M: time.Date(2000, time.February, 5, 4, 4, 4, 0, time.UTC),
61+
}),
62+
)
63+
64+
// 2019-12-16 17:31:51.769 [INFO] <example_test.go:56> check out my structure {"hello": {"meow": 1, "bar": "barbar", "m": "2000-02-05T04:04:04Z"}}
65+
}
66+
4767
func Example_testing() {
4868
// Provided by the testing package in tests.
4969
var t testing.TB
@@ -66,17 +86,16 @@ func Example_tracing() {
6686
}
6787

6888
func Example_multiple() {
69-
ctx := context.Background()
7089
l := sloghuman.Make(os.Stdout)
7190

7291
f, err := os.OpenFile("stackdriver", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
7392
if err != nil {
74-
l.Fatal(ctx, "failed to open stackdriver log file", slog.Error(err))
93+
l.Fatal(context.Background(), "failed to open stackdriver log file", slog.Error(err))
7594
}
7695

7796
l = slog.Make(l, slogstackdriver.Make(f))
7897

79-
l.Info(ctx, "log to stdout and stackdriver")
98+
l.Info(context.Background(), "log to stdout and stackdriver")
8099

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

map.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ var _ json.Marshaler = Map(nil)
3232
//
3333
// 5. slices and arrays go through the encode function for every element.
3434
//
35-
// 6. If the value cannot be encoded directly with json.Marshal (like channels)
36-
// then fmt.Sprintf("%+v") is used.
35+
// 6. For values that cannot be encoded with json.Marshal, fmt.Sprintf("%+v") is used.
3736
//
3837
// 7. json.Marshal(v) is used for all other values.
3938
func (m Map) MarshalJSON() ([]byte, error) {

map_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func TestMap(t *testing.T) {
9393
{
9494
"msg": "failed to marshal to JSON",
9595
"fun": "cdr.dev/slog.encodeJSON",
96-
"loc": "`+mapTestFile+`:132"
96+
"loc": "`+mapTestFile+`:131"
9797
},
9898
"json: error calling MarshalJSON for type slog_test.complexJSON: json: unsupported type: complex128"
9999
],

0 commit comments

Comments
 (0)