-
Notifications
You must be signed in to change notification settings - Fork 31
/
logger.go
148 lines (125 loc) · 3.4 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
package main
import (
"bytes"
"github.com/sirupsen/logrus"
)
// LoggerConfig is used for initialising the logger
type LoggerConfig struct {
Name string
Format LogFormat
Level LogLevel
AdditionalFields *map[string]interface{}
}
// InitLogger is used for setting up a new logger for a component
func InitLogger(config *LoggerConfig) *Logger {
log := logrus.New()
log.SetFormatter(config.Format.Get())
log.SetLevel(config.Level.Get())
fields := logrus.Fields{
"module": config.Name,
}
if config.AdditionalFields != nil {
for key, value := range *config.AdditionalFields {
fields[key] = value
}
}
logger := &Logger{
config: config,
instanceRaw: log,
instance: log.WithFields(fields),
}
return logger
}
// Logger represents the logger instance created on InitLogger
type Logger struct {
config *LoggerConfig
instance *logrus.Entry
instanceRaw *logrus.Logger
}
// SetOutput exists for characterisation testing
func (l *Logger) SetOutput(buffer *bytes.Buffer) {
l.instanceRaw.SetOutput(buffer)
}
// Trace logs at the trace level
func (l *Logger) Trace(log ...interface{}) {
l.instance.Trace(log...)
}
// Tracef logs at the trace level with formatting
func (l *Logger) Tracef(format string, log ...interface{}) {
l.instance.Tracef(format, log...)
}
// Debug logs at the debug level
func (l *Logger) Debug(log ...interface{}) {
l.instance.Debug(log...)
}
// Debugf logs at the debug level with formatting
func (l *Logger) Debugf(format string, log ...interface{}) {
l.instance.Debugf(format, log...)
}
// Info logs at the info level
func (l *Logger) Info(log ...interface{}) {
l.instance.Info(log...)
}
// Infof logs at the info level with formatting
func (l *Logger) Infof(format string, log ...interface{}) {
l.instance.Infof(format, log...)
}
// Warn logs at the warn level
func (l *Logger) Warn(log ...interface{}) {
l.instance.Warn(log...)
}
// Warnf logs at the warn level with formatting
func (l *Logger) Warnf(format string, log ...interface{}) {
l.instance.Warnf(format, log...)
}
// Error logs at the error level
func (l *Logger) Error(log ...interface{}) {
l.instance.Error(log...)
}
// Errorf logs at the error level with formatting
func (l *Logger) Errorf(format string, log ...interface{}) {
l.instance.Errorf(format, log...)
}
// Fatal logs at the fatal level
func (l *Logger) Fatal(log ...interface{}) {
l.instance.Fatal(log...)
}
// Fatalf logs at the fatal level with formatting
func (l *Logger) Fatalf(format string, log ...interface{}) {
l.instance.Fatalf(format, log...)
}
// Panic logs at the panic level
func (l *Logger) Panic(log ...interface{}) {
l.instance.Panic(log...)
}
// Panicf logs at the panic level with formatting
func (l *Logger) Panicf(format string, log ...interface{}) {
l.instance.Panicf(format, log...)
}
// LogLevel is a string represent of the log level
type LogLevel string
// String implements the string return type for LogLevel
func (ll *LogLevel) String() string {
return string(*ll)
}
// Get retrieves the LogLevel for logrus to use
func (ll *LogLevel) Get() logrus.Level {
switch *ll {
case "trace":
return logrus.TraceLevel
case "debug":
return logrus.DebugLevel
case "info":
return logrus.InfoLevel
case "warn":
return logrus.WarnLevel
case "error":
return logrus.ErrorLevel
case "fatal":
return logrus.FatalLevel
case "panic":
return logrus.PanicLevel
default:
return logrus.TraceLevel
}
}