-
Notifications
You must be signed in to change notification settings - Fork 41
/
logrus_mate.go
221 lines (162 loc) · 3.92 KB
/
logrus_mate.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package logrus_mate
import (
"errors"
"io"
"strings"
"sync"
"github.com/gogap/config"
"github.com/sirupsen/logrus"
)
var (
ErrLoggerNotExist = errors.New("logger not exist")
)
type LogrusMate struct {
loggersConf sync.Map //map[string]*Config
loggers sync.Map //map[string]*logrus.Logger
}
func NewLogger(opts ...Option) (logger *logrus.Logger, err error) {
l := logrus.New()
if err = Hijack(l, opts...); err != nil {
return
}
return l, nil
}
func Hijack(logger *logrus.Logger, opts ...Option) (err error) {
logrusMateConf := Config{}
for _, o := range opts {
o(&logrusMateConf)
}
hijackConf := config.NewConfig(logrusMateConf.configOpts...)
return hijackByConfig(logger, hijackConf)
}
func hijackByConfig(logger *logrus.Logger, conf config.Configuration) (err error) {
if conf == nil {
return
}
outConf := conf.GetConfig("out")
formatterConf := conf.GetConfig("formatter")
outName := "stdout"
formatterName := "text"
var outOptionsConf, formatterOptionsConf config.Configuration
if outConf != nil {
outName = outConf.GetString("name", "stdout")
outOptionsConf = outConf.GetConfig("options")
}
if formatterConf != nil {
formatterName = formatterConf.GetString("name", "text")
formatterOptionsConf = formatterConf.GetConfig("options")
}
var out io.Writer
if out, err = NewWriter(outName, outOptionsConf); err != nil {
return
}
var formatter logrus.Formatter
if formatter, err = NewFormatter(formatterName, formatterOptionsConf); err != nil {
return
}
var hooks []logrus.Hook
confHooks := conf.GetConfig("hooks")
if confHooks != nil {
hookNames := confHooks.Keys()
for i := 0; i < len(hookNames); i++ {
var hook logrus.Hook
if hook, err = NewHook(hookNames[i], confHooks.GetConfig(hookNames[i])); err != nil {
return
}
hooks = append(hooks, hook)
}
}
level := conf.GetString("level")
if len(level) == 0 {
level = "info"
}
var lvl = logrus.DebugLevel
if lvl, err = logrus.ParseLevel(level); err != nil {
return
}
l := logrus.New()
l.Level = lvl
l.Out = out
l.Formatter = formatter
for i := 0; i < len(hooks); i++ {
l.Hooks.Add(hooks[i])
}
*logger = *l
return
}
func NewLogrusMate(opts ...Option) (logrusMate *LogrusMate, err error) {
mate := &LogrusMate{
loggersConf: sync.Map{},
loggers: sync.Map{},
}
logrusMateConf := Config{}
for _, o := range opts {
o(&logrusMateConf)
}
conf := config.NewConfig(logrusMateConf.configOpts...)
if conf == nil {
logrusMate = mate
return
}
loggerNames := conf.Keys()
for i := 0; i < len(loggerNames); i++ {
mate.loggersConf.LoadOrStore(loggerNames[i], conf.GetConfig(loggerNames[i]))
}
logrusMate = mate
return
}
func (p *LogrusMate) Hijack(logger *logrus.Logger, loggerName string, opts ...Option) (err error) {
confV, exist := p.loggersConf.Load(loggerName)
if !exist {
err = ErrLoggerNotExist
return
}
conf := confV.(config.Configuration)
if len(opts) > 0 {
newConf := Config{}
for _, o := range opts {
o(&newConf)
}
conf2 := config.NewConfig(newConf.configOpts...)
err = hijackByConfig(
logger,
conf.WithFallback(conf2.Configuration),
)
return
}
err = hijackByConfig(logger, confV.(config.Configuration))
return
}
func (p *LogrusMate) Logger(loggerName ...string) (logger *logrus.Logger) {
name := "default"
if len(loggerName) > 0 {
name = strings.TrimSpace(loggerName[0])
if len(name) == 0 {
name = "default"
}
}
lv, exist := p.loggers.Load(name)
if exist {
return lv.(*logrus.Logger)
}
confV, exist := p.loggersConf.Load(name)
if !exist {
return nil
}
l := logrus.New()
if err := hijackByConfig(l, confV.(config.Configuration)); err != nil {
return nil
}
p.loggers.LoadOrStore(name, l)
return l
}
func (p *LogrusMate) LoggerNames() []string {
var keys []string
p.loggersConf.Range(func(key, value interface{}) bool {
if kk, ok := key.(string); ok {
keys = append(keys, kk)
}
return true
})
return keys
}