Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
74 lines (53 loc) · 1.88 KB
/
Copy pathexample_test.go
File metadata and controls
74 lines (53 loc) · 1.88 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
67
68
69
70
71
72
73
74
package logger_test
import (
"context"
"fmt"
stdlog "log"
"os"
"github.com/hamba/logger/v2"
"github.com/hamba/logger/v2/ctx"
)
func ExampleNew() {
log := logger.New(os.Stdout, logger.LogfmtFormat(), logger.Info).With(ctx.Str("env", "prod"))
log.Info("redis connection", ctx.Str("redis", "some redis name"), ctx.Int("timeout", 10))
log.Debug("dropped, more verbose than Info")
// Output:
// lvl=info msg="redis connection" env=prod redis="some redis name" timeout=10
}
func ExampleSyncWriter() {
log := logger.New(logger.NewSyncWriter(os.Stdout), logger.LogfmtFormat(), logger.Info).With(ctx.Str("env", "prod"))
log.Info("redis connection", ctx.Str("redis", "some redis name"), ctx.Int("timeout", 10))
// Output:
// lvl=info msg="redis connection" env=prod redis="some redis name" timeout=10
}
func ExampleWithContext() {
log := logger.New(os.Stdout, logger.LogfmtFormat(), logger.Info).With(ctx.Str("svc", "api"))
reqCtx := logger.WithContext(context.Background(), log, ctx.Str("req_id", "abc-123"), ctx.Str("method", "GET"))
reqLog := log.FromContext(reqCtx)
reqLog.Info("request handled", ctx.Int("status", 200))
// Output:
// lvl=info msg="request handled" svc=api req_id=abc-123 method=GET status=200
}
func ExampleLogger_Writer() {
log := logger.New(os.Stdout, logger.LogfmtFormat(), logger.Info)
std := stdlog.New(log.Writer(logger.Warn), "", 0)
std.Println("disk almost full")
// Output:
// lvl=warn msg="disk almost full"
}
func ExampleLogger_WithTimestamp() {
log := logger.New(os.Stdout, logger.LogfmtFormat(), logger.Info)
// The cancel function must be called to stop the clock.
cancel := log.WithTimestamp()
defer cancel()
log.Info("started")
}
func ExampleLevelFromString() {
lvl, err := logger.LevelFromString("dbug")
fmt.Println(lvl, err)
_, err = logger.LevelFromString("verbose")
fmt.Println(err)
// Output:
// dbug <nil>
// unknown level verbose
}