Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
66 lines (57 loc) · 1.81 KB
/
Copy pathexample_test.go
File metadata and controls
66 lines (57 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package log_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log/slog"
"github.com/jgolang/log"
)
// func ExampleStackTrace_stacktrace() {
// // Use this function to see the trace of execution of the sentence.
// // This function is useful for tracking where errors are generated.
// func() {
// log.StackTrace("My message...")
// }()
// // Output:
// // 2020/07/05 21:27:22 INFO example_test.go:43 (func1) My message...
// // --- TRACE:
// // /Users/me/myWorkdir/example/fileA.go:43 func1()
// // /Users/me/myWorkdir/example/fileA.go:43 ExampleStackTrace()
// // /Users/me/myWorkdir/example/fileB.go:20 funcLevel1()
// // /Users/me/myWorkdir/example/fileC.go:17 funcLevel2()
// // ...
// // ---
// }
func ExampleInfo_info() {
// Use this function to see the trace of execution of the sentence.
// This function is useful for tracking where errors are generated.
log.Info("Hello world!")
}
// serviceHandler tags every record with the service name before the handler
// it wraps encodes it.
type serviceHandler struct {
slog.Handler
}
func (h serviceHandler) Handle(ctx context.Context, record slog.Record) error {
record.AddAttrs(slog.String("service", "billing"))
return h.Handler.Handle(ctx, record)
}
func ExampleWithHandlerWrapper() {
var buf bytes.Buffer
logger := log.New(
// The wrapper applies to the JSON handler whatever the option order.
log.WithHandlerWrapper(func(next slog.Handler) slog.Handler {
return serviceHandler{Handler: next}
}),
log.WithJSONHandler(&buf),
)
logger.Info("payment captured")
var record struct{ Level, Msg, Service string }
if err := json.Unmarshal(buf.Bytes(), &record); err != nil {
fmt.Println(err)
return
}
fmt.Println(record.Level, record.Msg, record.Service)
// Output: INFO payment captured billing
}