-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexample_test.go
47 lines (40 loc) · 1.35 KB
/
example_test.go
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
package golog_test
import (
"bufio"
"context"
"fmt"
"os"
"time"
"github.com/damianopetrungaro/golog"
)
func ExampleLogger() {
ctx := context.Background()
t, err := time.Parse("2006", "2021")
if err != nil {
fmt.Println(err)
return
}
w := golog.NewBufWriter(
golog.NewJsonEncoder(golog.DefaultJsonConfig()),
bufio.NewWriter(os.Stdout),
golog.DefaultErrorHandler(),
golog.DEBUG,
)
defer w.Flush()
golog.SetLogger(golog.New(w, golog.NewLevelCheckerOption(golog.DEBUG)))
logger := golog.With(golog.String("hello", "world"))
logger.Error(ctx, "an error message")
logger.Error(ctx, `another error
message`)
loggerWithErr := logger.With(golog.Err(fmt.Errorf("error: ops!")))
logger.Info(ctx, "an info message")
loggerWithErr.Warn(ctx, "a warning message")
loggerWithErr.With(golog.Mapper("user", user{ID: "uuid", Reference: 123, Birthdate: t})).Error(ctx, "a warning message")
// Output:
// {"level":"ERROR","message":"an error message","hello":"world"}
// {"level":"ERROR","message":"another error \nmessage","hello":"world"}
// {"level":"INFO","message":"an info \tmessage","hello":"world"}
// {"level":"WARN","message":"a warning message","hello":"world","error":"error: ops!"}
// {"level":"ERROR","message":"a warning message","hello":"world","error":"error: ops!","user":{"id":"uuid","ref":123,"birthdate":"2021-01-01T00:00:00Z"}}
//
}