-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
276 lines (240 loc) · 7.14 KB
/
logger.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
package context
import (
"fmt"
"github.com/procyon-projects/procyon-configure"
"github.com/sirupsen/logrus"
"os"
"strings"
)
type LoggingConfiguration interface {
ApplyLoggingProperties(properties configure.LoggingProperties)
}
type LogLevel uint32
const (
PanicLevel LogLevel = iota
FatalLevel
ErrorLevel
WarnLevel
InfoLevel
DebugLevel
TraceLevel
)
type Logger interface {
Trace(ctx interface{}, message interface{})
Debug(ctx interface{}, message interface{})
Info(ctx interface{}, message interface{})
Warning(ctx interface{}, message interface{})
Error(ctx interface{}, message interface{})
Fatal(ctx interface{}, message interface{})
Panic(ctx interface{}, message interface{})
Print(ctx interface{}, message interface{})
Tracef(ctx interface{}, format string, args ...interface{})
Debugf(ctx interface{}, format string, args ...interface{})
Infof(ctx interface{}, format string, args ...interface{})
Warningf(ctx interface{}, format string, args ...interface{})
Errorf(ctx interface{}, format string, args ...interface{})
Fatalf(ctx interface{}, format string, args ...interface{})
Panicf(ctx interface{}, format string, args ...interface{})
Printf(ctx interface{}, format string, args ...interface{})
}
type SimpleLogger struct {
log *logrus.Logger
}
func NewSimpleLogger() *SimpleLogger {
log := &SimpleLogger{
&logrus.Logger{
Out: os.Stdout,
Formatter: NewLogFormatter(),
Level: logrus.InfoLevel,
},
}
return log
}
func (l *SimpleLogger) ApplyLoggingProperties(properties configure.LoggingProperties) {
loggerLevel := logrus.DebugLevel
switch properties.Level {
case "TRACE":
loggerLevel = logrus.TraceLevel
case "DEBUG":
loggerLevel = logrus.DebugLevel
case "INFO":
loggerLevel = logrus.InfoLevel
case "ERROR":
loggerLevel = logrus.ErrorLevel
case "WARNING":
loggerLevel = logrus.WarnLevel
case "FATAL":
loggerLevel = logrus.FatalLevel
case "PANIC":
loggerLevel = logrus.PanicLevel
default:
loggerLevel = logrus.DebugLevel
}
l.log.Level = loggerLevel
fullLogPath := properties.FilePath + properties.FileName
if fullLogPath != "" {
logFile, err := os.OpenFile(fullLogPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic("Could Not Open Log File : " + err.Error())
}
l.log.SetOutput(logFile)
} else {
l.log.Formatter.(*LogFormatter).isTerminal = true
}
}
func (l *SimpleLogger) Trace(ctx interface{}, message interface{}) {
l.logMessage(ctx, TraceLevel, message)
}
func (l *SimpleLogger) Debug(ctx interface{}, message interface{}) {
l.logMessage(ctx, DebugLevel, message)
}
func (l *SimpleLogger) Info(ctx interface{}, message interface{}) {
l.logMessage(ctx, InfoLevel, message)
}
func (l SimpleLogger) Warning(ctx interface{}, message interface{}) {
l.logMessage(ctx, WarnLevel, message)
}
func (l *SimpleLogger) Error(ctx interface{}, message interface{}) {
l.logMessage(ctx, ErrorLevel, message)
}
func (l *SimpleLogger) Fatal(ctx interface{}, message interface{}) {
l.logMessage(ctx, FatalLevel, message)
}
func (l *SimpleLogger) Panic(ctx interface{}, message interface{}) {
l.logMessage(ctx, PanicLevel, message)
}
func (l *SimpleLogger) Print(ctx interface{}, message interface{}) {
l.log.Print(message)
}
func (l *SimpleLogger) Tracef(ctx interface{}, format string, args ...interface{}) {
l.logMessageWithFormat(ctx, TraceLevel, format, args...)
}
func (l *SimpleLogger) Debugf(ctx interface{}, format string, args ...interface{}) {
l.logMessageWithFormat(ctx, DebugLevel, format, args...)
}
func (l *SimpleLogger) Infof(ctx interface{}, format string, args ...interface{}) {
l.logMessageWithFormat(ctx, InfoLevel, format, args...)
}
func (l SimpleLogger) Warningf(ctx interface{}, format string, args ...interface{}) {
l.logMessageWithFormat(ctx, WarnLevel, format, args...)
}
func (l *SimpleLogger) Errorf(ctx interface{}, format string, args ...interface{}) {
l.logMessageWithFormat(ctx, ErrorLevel, format, args...)
}
func (l *SimpleLogger) Fatalf(ctx interface{}, format string, args ...interface{}) {
l.logMessageWithFormat(ctx, FatalLevel, format, args...)
}
func (l *SimpleLogger) Panicf(ctx interface{}, format string, args ...interface{}) {
l.logMessageWithFormat(ctx, PanicLevel, format, args...)
}
func (l *SimpleLogger) Printf(ctx interface{}, format string, args ...interface{}) {
l.log.Printf(format, args...)
}
func (l *SimpleLogger) logMessage(ctx interface{}, level LogLevel, message interface{}) {
entry := l.getLogEntry(ctx, level)
switch level {
case TraceLevel:
entry.Trace(message)
case DebugLevel:
entry.Debug(message)
case InfoLevel:
entry.Info(message)
case WarnLevel:
entry.Warn(message)
case ErrorLevel:
entry.Error(message)
case FatalLevel:
entry.Fatal(message)
case PanicLevel:
entry.Panic(message)
}
}
func (l *SimpleLogger) logMessageWithFormat(ctx interface{}, level LogLevel, format string, args ...interface{}) {
entry := l.getLogEntry(ctx, level)
switch level {
case TraceLevel:
entry.Tracef(format, args...)
case DebugLevel:
entry.Debugf(format, args...)
case InfoLevel:
entry.Infof(format, args...)
case WarnLevel:
entry.Warnf(format, args...)
case ErrorLevel:
entry.Errorf(format, args...)
case FatalLevel:
entry.Fatalf(format, args...)
case PanicLevel:
entry.Panicf(format, args...)
}
}
func (l *SimpleLogger) getLogEntry(ctx interface{}, level LogLevel) *logrus.Entry {
if ctx == nil {
panic("Context cannot be nil")
}
var entry *logrus.Entry
switch ctx.(type) {
case Context:
entry = l.log.WithFields(logrus.Fields{
"CONTEXT_ID": ctx.(Context).GetContextId(),
"LEVEL_COLOR": l.getLevelColor(level),
})
case ContextId:
entry = l.log.WithFields(logrus.Fields{
"CONTEXT_ID": ctx,
"LEVEL_COLOR": l.getLevelColor(level),
})
default:
panic("First parameter must be Context or Context Id")
}
return entry
}
func (l *SimpleLogger) getLevelColor(logLevel LogLevel) int {
var levelColor int
switch logLevel {
case DebugLevel, TraceLevel:
levelColor = 37 // gray
case WarnLevel:
levelColor = 33 // yellow
case ErrorLevel, FatalLevel, PanicLevel:
levelColor = 31 // red
case InfoLevel:
levelColor = 36 // blue
default:
levelColor = 37
}
return levelColor
}
type LogFormatter struct {
logrus.TextFormatter
isTerminal bool
logFormat string
}
func NewLogFormatter() *LogFormatter {
formatter := &LogFormatter{}
formatter.TimestampFormat = "2006-01-02 15:04:05.000"
formatter.logFormat = "[%s] %s %s : %s\n"
return formatter
}
func (f *LogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
contextId := entry.Data["CONTEXT_ID"]
if contextId == nil {
return []byte(entry.Message + "\n"), nil
}
return []byte(
fmt.Sprintf(f.logFormat, entry.Time.Format(f.TimestampFormat),
f.GetLevelString(entry),
f.GetSumContextId(contextId.(ContextId)),
entry.Message)), nil
}
func (f *LogFormatter) GetSumContextId(contextId ContextId) string {
var sumContextId = string(contextId)
return sumContextId[:8]
}
func (f *LogFormatter) GetLevelString(entry *logrus.Entry) string {
levelString := strings.ToUpper(entry.Level.String())
if !f.isTerminal {
return fmt.Sprintf("%-7s", levelString)
}
return fmt.Sprintf("\x1b[%dm%-7s\x1b[0m", entry.Data["LEVEL_COLOR"], levelString)
}