forked from ziflex/lecho
-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
88 lines (71 loc) · 1.66 KB
/
options.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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package lecho
import (
"github.com/labstack/gommon/log"
"github.com/rs/zerolog"
)
type (
Options struct {
context zerolog.Context
level log.Lvl
prefix string
}
Setter func(opts *Options)
)
func newOptions(log zerolog.Logger, setters []Setter) *Options {
elvl, _ := MatchZeroLevel(log.GetLevel())
opts := &Options{
context: log.With(),
level: elvl,
}
for _, set := range setters {
set(opts)
}
return opts
}
func WithLevel(level log.Lvl) Setter {
return func(opts *Options) {
zlvl, elvl := MatchEchoLevel(level)
opts.context = opts.context.Logger().Level(zlvl).With()
opts.level = elvl
}
}
func WithField(name string, value interface{}) Setter {
return func(opts *Options) {
opts.context = opts.context.Interface(name, value)
}
}
func WithFields(fields map[string]interface{}) Setter {
return func(opts *Options) {
opts.context = opts.context.Fields(fields)
}
}
func WithTimestamp() Setter {
return func(opts *Options) {
opts.context = opts.context.Timestamp()
}
}
func WithCaller() Setter {
return func(opts *Options) {
opts.context = opts.context.Caller()
}
}
func WithCallerWithSkipFrameCount(skipFrameCount int) Setter {
return func(opts *Options) {
opts.context = opts.context.CallerWithSkipFrameCount(skipFrameCount)
}
}
func WithPrefix(prefix string) Setter {
return func(opts *Options) {
opts.context = opts.context.Str("prefix", prefix)
}
}
func WithHook(hook zerolog.Hook) Setter {
return func(opts *Options) {
opts.context = opts.context.Logger().Hook(hook).With()
}
}
func WithHookFunc(hook zerolog.HookFunc) Setter {
return func(opts *Options) {
opts.context = opts.context.Logger().Hook(hook).With()
}
}