Skip to content

Commit db2683c

Browse files
committed
Add ForceJSON example
Closes #58
1 parent a4f66ba commit db2683c

File tree

6 files changed

+42
-10
lines changed

6 files changed

+42
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Here is a list of reasons how we improved on zap with slog.
9292

9393
1. Structured logging of Go structures with `json.Marshal`
9494
- All values will be logged with `json.Marshal` unless they implement `fmt.Stringer` or `error`.
95-
- You can force JSON by using [`slog.JSON`](https://godoc.org/cdr.dev/slog#JSON).
95+
- You can force JSON by using [`slog.ForceJSON`](https://godoc.org/cdr.dev/slog#ForceJSON).
9696
- One may implement [`slog.Value`](https://godoc.org/cdr.dev/slog#Value) to override the representation completely.
9797
- With zap, We found ourselves often implementing zap's
9898
[ObjectMarshaler](https://godoc.org/go.uber.org/zap/zapcore#ObjectMarshaler) to log Go structures. This was

example_force_json_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package slog_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"cdr.dev/slog"
9+
"cdr.dev/slog/sloggers/sloghuman"
10+
)
11+
12+
type stringer struct {
13+
X int `json:"x"`
14+
}
15+
16+
func (s *stringer) String() string {
17+
return fmt.Sprintf("string method: %v", s.X)
18+
}
19+
20+
func (s *stringer) SlogValue() interface{} {
21+
return slog.ForceJSON(s)
22+
}
23+
24+
func ExampleForceJSON() {
25+
l := sloghuman.Make(os.Stdout)
26+
27+
l.Info(context.Background(), "hello", slog.F("stringer", &stringer{X: 3}))
28+
29+
// 2019-12-06 23:33:40.263 [INFO] <example_force_json_test.go:27> hello {"stringer": {"x": 3}}
30+
}
File renamed without changes.

map.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ func (m Map) MarshalJSON() ([]byte, error) {
3434
return b.Bytes(), nil
3535
}
3636

37-
// JSON ensures the value is logged via json.Marshal even
38-
// in the presence of the fmt.Stringer and error interfaces.
39-
func JSON(v interface{}) interface{} {
37+
// ForceJSON ensures the value is logged via json.Marshal even
38+
// if it implements fmt.Stringer or error.
39+
//
40+
// See the ForceJSON example.
41+
func ForceJSON(v interface{}) interface{} {
4042
return jsonVal{v: v}
4143
}
4244

@@ -70,7 +72,7 @@ func marshalArray(a []interface{}) []byte {
7072
func encode(v interface{}) []byte {
7173
switch v := v.(type) {
7274
case Value:
73-
return encode(v.LogValue())
75+
return encode(v.SlogValue())
7476
case []interface{}:
7577
return marshalArray(v)
7678
case xerrors.Formatter:

map_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func TestMap(t *testing.T) {
9292
{
9393
"msg": "failed to marshal to JSON",
9494
"fun": "cdr.dev/slog.encode",
95-
"loc": "`+mapTestFile+`:84"
95+
"loc": "`+mapTestFile+`:86"
9696
},
9797
"json: unsupported type: func(*testing.T, string) string"
9898
],
@@ -148,7 +148,7 @@ func TestMap(t *testing.T) {
148148
t.Parallel()
149149

150150
test(t, slog.M(
151-
slog.F("error", slog.JSON(io.EOF)),
151+
slog.F("error", slog.ForceJSON(io.EOF)),
152152
), `{
153153
"error": {}
154154
}`)
@@ -169,7 +169,7 @@ type meow struct {
169169
a int
170170
}
171171

172-
func (m meow) LogValue() interface{} {
172+
func (m meow) SlogValue() interface{} {
173173
return "xdxd"
174174
}
175175

slog.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ func M(fs ...Field) Map {
3434
}
3535

3636
// Value represents a log value.
37-
// Implement LogValue in your own types to override
37+
// Implement SlogValue in your own types to override
3838
// the value encoded when logging.
3939
type Value interface {
40-
LogValue() interface{}
40+
SlogValue() interface{}
4141
}
4242

4343
// Error is the standard key used for logging a Go error value.

0 commit comments

Comments
 (0)