-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger_options.go
64 lines (55 loc) · 1.48 KB
/
logger_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
package zap_log
import (
"fmt"
"git.golaxy.org/core"
"git.golaxy.org/core/util/option"
"go.uber.org/zap"
)
// LoggerOptions 所有选项
type LoggerOptions struct {
ZapLogger *zap.Logger
ServiceInfo bool
RuntimeInfo bool
CallerSkip int
}
var With _Option
type _Option struct{}
// Default 默认值
func (_Option) Default() option.Setting[LoggerOptions] {
return func(options *LoggerOptions) {
With.ZapLogger(zap.NewExample())(options)
With.ServiceInfo(false)(options)
With.RuntimeInfo(false)(options)
With.CallerSkip(2)(options)
}
}
// ZapLogger zap logger
func (_Option) ZapLogger(logger *zap.Logger) option.Setting[LoggerOptions] {
return func(options *LoggerOptions) {
if logger == nil {
panic(fmt.Errorf("%w: option ZapLogger can't be assigned to nil", core.ErrArgs))
}
options.ZapLogger = logger
}
}
// ServiceInfo 添加service信息
func (_Option) ServiceInfo(b bool) option.Setting[LoggerOptions] {
return func(options *LoggerOptions) {
options.ServiceInfo = b
}
}
// RuntimeInfo 添加runtime信息
func (_Option) RuntimeInfo(b bool) option.Setting[LoggerOptions] {
return func(options *LoggerOptions) {
options.RuntimeInfo = b
}
}
// CallerSkip 调用堆栈skip值,用于打印调用堆栈信息
func (_Option) CallerSkip(skip int) option.Setting[LoggerOptions] {
return func(options *LoggerOptions) {
if skip < 0 {
panic(fmt.Errorf("%w: option CallerSkip can't be set to a value less than 0", core.ErrArgs))
}
options.CallerSkip = skip
}
}