-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger_option.go
90 lines (73 loc) · 1.74 KB
/
logger_option.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
89
90
package logrusKit
import (
"github.com/sirupsen/logrus"
"io"
"os"
)
type (
loggerOptions struct {
// formatter 日志格式
formatter logrus.Formatter
// reportCaller 默认: true
reportCaller bool
// level 日志级别,默认: logrus.DebugLevel
level logrus.Level
// output 默认: os.Stderr
output io.Writer
// msgPrefix 日志输出的msg属性的前缀,(1) 默认: ""; (2) 非空的话,会拼接在msg前面
msgPrefix string
// disableQuote 默认: false
disableQuote bool
}
LoggerOption func(opts *loggerOptions)
)
func loadOptions(options ...LoggerOption) *loggerOptions {
/* 默认值s */
opts := &loggerOptions{
formatter: nil,
reportCaller: true,
level: logrus.DebugLevel,
// 默认: 输出到控制台
output: os.Stderr,
msgPrefix: "",
disableQuote: false,
}
for _, option := range options {
option(opts)
}
// 容错,以防"调用方"瞎传
if opts.formatter == nil {
opts.formatter = NewDefaultTextFormatter()
}
return opts
}
func WithFormatter(formatter logrus.Formatter) LoggerOption {
return func(opts *loggerOptions) {
opts.formatter = formatter
}
}
func WithReportCaller(reportCaller bool) LoggerOption {
return func(opts *loggerOptions) {
opts.reportCaller = reportCaller
}
}
func WithLevel(level logrus.Level) LoggerOption {
return func(opts *loggerOptions) {
opts.level = level
}
}
func WithOutput(output io.Writer) LoggerOption {
return func(opts *loggerOptions) {
opts.output = output
}
}
func WithMsgPrefix(msgPrefix string) LoggerOption {
return func(opts *loggerOptions) {
opts.msgPrefix = msgPrefix
}
}
func WithDisableQuote(disableQuote bool) LoggerOption {
return func(opts *loggerOptions) {
opts.disableQuote = disableQuote
}
}