/
options.go
153 lines (133 loc) · 3.73 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package loggerhelper
import (
"fmt"
"io"
"time"
logrus "github.com/sirupsen/logrus"
)
// redis类型
type FormatType int32
const (
FormatType_JSON FormatType = 0
FormatType_Text FormatType = 1
)
//Option 设置key行为的选项
//@attribute MaxTTL time.Duration 为0则不设置过期
//@attribute AutoRefresh string 需要为crontab格式的字符串,否则不会自动定时刷新
type Options struct {
Type FormatType
DisableTimeField bool
TimeFormat string
Level logrus.Level
DefaultFieldMap logrus.FieldMap
ExtFields map[string]interface{}
Output io.Writer
Hooks []logrus.Hook
}
var DefaultOpts = Options{
Type: FormatType_JSON,
TimeFormat: time.RFC3339Nano,
Level: logrus.DebugLevel,
DefaultFieldMap: logrus.FieldMap{
logrus.FieldKeyTime: "time",
logrus.FieldKeyLevel: "level",
logrus.FieldKeyMsg: "event",
logrus.FieldKeyLogrusError: "logrus_error",
logrus.FieldKeyFunc: "caller",
logrus.FieldKeyFile: "file",
},
ExtFields: map[string]interface{}{},
Hooks: []logrus.Hook{},
}
// Option configures how we set up the connection.
type Option interface {
Apply(*Options)
}
// func (emptyOption) apply(*Options) {}
type funcOption struct {
f func(*Options)
}
func (fo *funcOption) Apply(do *Options) {
fo.f(do)
}
func newFuncOption(f func(*Options)) *funcOption {
return &funcOption{
f: f,
}
}
//WithTextFormat Init函数的参数,用于设置使用text格式替换json格式
func WithTextFormat() Option {
return newFuncOption(func(o *Options) {
o.Type = FormatType_Text
})
}
//WithDisableTimeField Init函数的参数,用于设置使用text格式替换json格式
func WithDisableTimeField() Option {
return newFuncOption(func(o *Options) {
o.DisableTimeField = true
})
}
//WithTimeFormat Init函数的参数,用于设置使用指定的时间解析格式,默认为RFC3339Nano
func WithTimeFormat(TimeFormat string) Option {
return newFuncOption(func(o *Options) {
o.TimeFormat = TimeFormat
})
}
//parseLevel 将字符串转为`logrus.Level`,未知的字符串默认匹配为"logrus.DebugLevel"
func parseLevel(loglevel string) logrus.Level {
level, err := logrus.ParseLevel(loglevel)
if err != nil {
fmt.Printf("未知的等级`%s`,使用默认值`Debug`\n", loglevel)
return logrus.DebugLevel
}
return level
}
//WithLevel Init函数的参数,用于设置log等级
func WithLevel(loglevel string) Option {
return newFuncOption(func(o *Options) {
o.Level = parseLevel(loglevel)
})
}
//AddHooks Init函数的参数,用于增加钩子
func AddHooks(hooks ...logrus.Hook) Option {
return newFuncOption(func(o *Options) {
if o.Hooks == nil {
o.Hooks = []logrus.Hook{}
}
o.Hooks = append(o.Hooks, hooks...)
})
}
//WithDefaultFieldMap Init函数的参数,用于设置默认字段的新命名
func WithDefaultFieldMap(fm logrus.FieldMap) Option {
return newFuncOption(func(o *Options) {
o.DefaultFieldMap = fm
})
}
//AddExtField Init函数的参数,用于增加扩展字段
func AddExtField(field string, value interface{}) Option {
return newFuncOption(func(o *Options) {
if o.ExtFields == nil {
o.ExtFields = map[string]interface{}{field: value}
} else {
o.ExtFields[field] = value
}
})
}
//WithExtFields Init函数的参数,用于设置扩展字段
func WithExtFields(extFields map[string]interface{}) Option {
return newFuncOption(func(o *Options) {
if o.ExtFields == nil || len(o.ExtFields) == 0 {
o.ExtFields = extFields
} else {
for field, value := range extFields {
o.ExtFields[field] = value
}
}
})
}
//WithOutput Init函数的参数,用于设置log的写入io
func WithOutput(writer io.Writer) Option {
return newFuncOption(func(o *Options) {
o.Output = writer
})
}