-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathruslog.go
318 lines (263 loc) · 8.3 KB
/
ruslog.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package ruslog
import (
"fmt"
"reflect"
"runtime"
"strings"
"sync"
"github.com/Sirupsen/logrus"
)
const (
ruslogFileName = "github.com/dogenzaka/ruslog/ruslog.go"
appenderFileName = "github.com/dogenzaka/ruslog/appender.go"
)
type (
Logger struct {
Name string // logger name(uniq,required)
Type string // ruslog.APPENDER_XXXX
Format string // ruslog.FORMATTER_XXXX
Level string // logrus.XXXXLevel.String()
FilePath string // output file path (optional)
RotationSize int64 // size threshold of the rotation example 10M) 1024 * 1024 * 10 (optional)
MaxRotation int // maximum count of the rotation (optional)
AddFileInfo bool // add the file info to the log message (optional)
Call func(level string, options map[string]interface{}, messages []string)
Callf func(level string, options map[string]interface{}, format string, args ...interface{})
sync.Mutex
logrus *logrus.Logger
}
logging struct {
loggers map[string]*Logger
}
appenders map[string]*Appender
formatters map[string]*Formatter
)
var (
// debug flag
DEBUG bool = false
// ruslog package instance
Logging *logging = &logging{
loggers: make(map[string]*Logger),
}
// Manage ruslog(logrus) Appenders
Appenders = appenders{
APPENDER_DEFAULT: &Appender{
Name: APPENDER_DEFAULT,
Setup: defaultAppender,
},
APPENDER_SIZE: &Appender{
Name: APPENDER_SIZE,
Setup: sizeRollingFileAppender,
},
APPENDER_DAILY: &Appender{
Name: APPENDER_DAILY,
Setup: dailyRollingFileAppender,
},
}
// Manage ruslog(logrus) formatters
Formatters = formatters{
FORMATTER_SIMPLE: &Formatter{
Name: FORMATTER_SIMPLE,
Formatter: &SimpleFormatter{},
},
//logrus
FORMATTER_TEXT: &Formatter{
Name: FORMATTER_TEXT,
Formatter: &logrus.TextFormatter{},
},
// logrus
FORMATTER_JSON: &Formatter{
Name: FORMATTER_JSON,
Formatter: &logrus.JSONFormatter{},
},
}
)
// load ruslog
func Configure(loggers []*Logger) *logging {
for _, logger := range loggers {
Logging.loggers[logger.Name] = logger.Setup()
if DEBUG {
fmt.Printf("[RUSLOG-INFO] Add logging. %s=%s\n", logger.Name, GetLevel(logger.Level))
}
}
return Logging
}
// Added the Formatter to manage
func AddFormatter(formatter *Formatter) *Formatter {
Formatters[formatter.Name] = formatter
return Formatters[formatter.Name]
}
// Added the Appender to manage
func AddAppender(appender *Appender) *Appender {
Appenders[appender.Name] = appender
return Appenders[appender.Name]
}
func GetLogger(name string) *Logger {
l := Logging.loggers[name]
// if name logger is not found, return default logger.
if l == nil {
l = &Logger{Name: name, Type: APPENDER_DEFAULT, Level: "Info", Format: FORMATTER_TEXT}
Configure([]*Logger{l})
}
return l
}
// Get the logging level value
func GetLevel(level string) logrus.Level {
l, err := logrus.ParseLevel(strings.ToLower(level))
if err != nil {
panic(err)
}
return l
}
// Call logger method for a given level
func CallMethod(logger *Logger, level string, message string, options map[string]interface{}) {
loggerLogrus := logger.logrus
entry := loggerLogrus.WithFields(options)
methodName := level
method := reflect.ValueOf(entry).MethodByName(methodName)
if method.IsValid() {
args := []reflect.Value{reflect.ValueOf(message)}
method.Call(args)
} else {
entry.Debug(message)
}
}
// -- Logger
// Setup appender
func (logger *Logger) Setup() *Logger {
appender := Appenders[logger.Type]
if appender == nil {
if DEBUG {
fmt.Println("[RUSLOG-INFO] Default logging.", APPENDER_DEFAULT)
}
appender = Appenders[APPENDER_DEFAULT]
}
return appender.Setup(logger)
}
///
// Debug log output (goroutine)
func (l *Logger) Debug(options map[string]interface{}, messages ...string) {
go l.Call("Debug", l.addFileInfo(options), messages)
}
// Info log output (goroutine)
func (l *Logger) Info(options map[string]interface{}, messages ...string) {
go l.Call("Info", l.addFileInfo(options), messages)
}
// Warn log output (goroutine)
func (l *Logger) Warn(options map[string]interface{}, messages ...string) {
go l.Call("Warn", l.addFileInfo(options), messages)
}
// Error log output (goroutine)
func (l *Logger) Error(options map[string]interface{}, messages ...string) {
go l.Call("Error", l.addFileInfo(options), messages)
}
// Fatal log output (goroutine)
func (l *Logger) Fatal(options map[string]interface{}, messages ...string) {
go l.Call("Fatal", l.addFileInfo(options), messages)
}
// Debugf log output (goroutine)
func (l *Logger) Debugf(options map[string]interface{}, format string, args ...interface{}) {
go l.Callf("Debug", l.addFileInfo(options), format, args...)
}
// Infof log output (goroutine)
func (l *Logger) Infof(options map[string]interface{}, format string, args ...interface{}) {
go l.Callf("Info", l.addFileInfo(options), format, args...)
}
// Warnf log output (goroutine)
func (l *Logger) Warnf(options map[string]interface{}, format string, args ...interface{}) {
go l.Callf("Warn", l.addFileInfo(options), format, args...)
}
// Errorf log output (goroutine)
func (l *Logger) Errorf(options map[string]interface{}, format string, args ...interface{}) {
go l.Callf("Error", l.addFileInfo(options), format, args...)
}
// Fatalf log output (goroutine)
func (l *Logger) Fatalf(options map[string]interface{}, format string, args ...interface{}) {
go l.Callf("Fatal", l.addFileInfo(options), format, args...)
}
// Debug log output (not goroutine)
func (l *Logger) DebugSync(options map[string]interface{}, messages ...string) {
l.Call("Debug", l.addFileInfo(options), messages)
}
// Info log output (not goroutine)
func (l *Logger) InfoSync(options map[string]interface{}, messages ...string) {
l.Call("Info", l.addFileInfo(options), messages)
}
// Warn log output (not goroutine)
func (l *Logger) WarnSync(options map[string]interface{}, messages ...string) {
l.Call("Warn", l.addFileInfo(options), messages)
}
// Error log output (not goroutine)
func (l *Logger) ErrorSync(options map[string]interface{}, messages ...string) {
l.Call("Error", l.addFileInfo(options), messages)
}
// Fatal log output (not goroutine)
func (l *Logger) FatalSync(options map[string]interface{}, messages ...string) {
l.Call("Fatal", l.addFileInfo(options), messages)
}
// Debugf log output (not goroutine)
func (l *Logger) DebugfSync(options map[string]interface{}, format string, args ...interface{}) {
l.Callf("Debug", l.addFileInfo(options), format, args...)
}
// Infof log output (not goroutine)
func (l *Logger) InfofSync(options map[string]interface{}, format string, args ...interface{}) {
l.Callf("Info", l.addFileInfo(options), format, args...)
}
// Warnf log output (not goroutine)
func (l *Logger) WarnfSync(options map[string]interface{}, format string, args ...interface{}) {
l.Callf("Warn", l.addFileInfo(options), format, args...)
}
// Errorf log output (not goroutine)
func (l *Logger) ErrorfSync(options map[string]interface{}, format string, args ...interface{}) {
l.Callf("Error", l.addFileInfo(options), format, args...)
}
// Fatalf log output (not goroutine)
func (l *Logger) FatalfSync(options map[string]interface{}, format string, args ...interface{}) {
l.Callf("Fatal", l.addFileInfo(options), format, args...)
}
///
// SetLevel sets the logger level.
func (l *Logger) SetLevel(level logrus.Level) {
l.Lock()
defer l.Unlock()
l.logrus.Level = level
}
// GetLevel returns the logger level.
func (l *Logger) GetLevel() logrus.Level {
l.Lock()
defer l.Unlock()
return l.logrus.Level
}
///
// log.Logger.Output like (gorutine)
func (l *Logger) Output(calldepth int, s string) error {
go l.Call("Info", nil, []string{s})
return nil
}
// io.Write like (gorutine)
func (l *Logger) Write(p []byte) (n int, err error) {
go l.logrus.Out.Write(p)
return 0, nil // The exception is ignored
}
///
// addFileInfo add the file info to the options if AddFileInfo is true.
func (l *Logger) addFileInfo(options map[string]interface{}) map[string]interface{} {
if !l.AddFileInfo {
return options
}
if options == nil {
options = map[string]interface{}{}
}
for depth := 1; ; depth++ {
_, file, line, ok := runtime.Caller(depth)
if !ok {
options["file"] = "unknown"
break
}
if !strings.HasSuffix(file, appenderFileName) && !strings.HasSuffix(file, ruslogFileName) {
options["file"] = fmt.Sprintf("%s:%d", file, line)
break
}
}
return options
}