-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger_global.go
208 lines (171 loc) · 6.49 KB
/
logger_global.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
package log
import (
"github.com/echocat/slf4g/level"
"github.com/echocat/slf4g/names"
"github.com/echocat/slf4g/fields"
)
// GetLogger returns the ROOT logger from the global Provider.
func GetRootLogger() Logger {
return GetProvider().GetRootLogger()
}
// GetLogger returns a logger for the given name from the global Provider.
// If instead of a string another object is given this will be used to create
// a logger name from it's package name.
func GetLogger(nameOrReference interface{}) Logger {
return GetProvider().GetLogger(names.FullLoggerNameGenerator(nameOrReference))
}
// GetLoggerForCurrentPackage is similar to GetLogger(name) but it extracts the
// name automatically from the current call stack. That means: this method
// should be only used for the package this logger should be for.
func GetLoggerForCurrentPackage() Logger {
return GetProvider().GetLogger(names.CurrentPackageLoggerNameGenerator(1))
}
// IsLevelEnabled checks if the given Level is enabled at the current root
// Logger.
func IsLevelEnabled(level level.Level) bool {
return GetRootLogger().IsLevelEnabled(level)
}
// Trace logs the provided arguments on Trace at the current root Logger.
func Trace(args ...interface{}) {
log(level.Trace, args...)
}
// Tracef is like Trace but wraps the message itself in a fmt.Sprintf action.
// By contract the actual format action will not be executed before the value
// will be really consumed.
func Tracef(format string, args ...interface{}) {
logf(level.Trace, format, args...)
}
// IsTraceEnabled checks if Trace is enabled at the current root Logger.
func IsTraceEnabled() bool {
return IsLevelEnabled(level.Trace)
}
// Debug logs the provided arguments on Debug at the current root Logger.
func Debug(args ...interface{}) {
log(level.Debug, args...)
}
// Debugf is like Debug but wraps the message itself in a fmt.Sprintf action.
// By contract the actual format action will not be executed before the value
// will be really consumed.
func Debugf(format string, args ...interface{}) {
logf(level.Debug, format, args...)
}
// IsDebugEnabled checks if Debug is enabled at the current root Logger.
func IsDebugEnabled() bool {
return IsLevelEnabled(level.Debug)
}
// Info logs the provided arguments on Info at the current root Logger.
func Info(args ...interface{}) {
log(level.Info, args...)
}
// Infof is like Info but wraps the message itself in a fmt.Sprintf action.
// By contract the actual format action will not be executed before the value
// will be really consumed.
func Infof(format string, args ...interface{}) {
logf(level.Info, format, args...)
}
// IsInfoEnabled checks if Info is enabled at the current root Logger.
func IsInfoEnabled() bool {
return IsLevelEnabled(level.Info)
}
// Warn logs the provided arguments on Warn at the current root Logger.
func Warn(args ...interface{}) {
log(level.Warn, args...)
}
// Warnf is like Warn but wraps the message itself in a fmt.Sprintf action.
// By contract the actual format action will not be executed before the value
// will be really consumed.
func Warnf(format string, args ...interface{}) {
logf(level.Warn, format, args...)
}
// IsWarnEnabled checks if Warn is enabled at the current root Logger.
func IsWarnEnabled() bool {
return IsLevelEnabled(level.Warn)
}
// Error logs the provided arguments on Error at the current root Logger.
func Error(args ...interface{}) {
log(level.Error, args...)
}
// Errorf is like Error but wraps the message itself in a fmt.Sprintf action.
// By contract the actual format action will not be executed before the value
// will be really consumed.
func Errorf(format string, args ...interface{}) {
logf(level.Error, format, args...)
}
// IsErrorEnabled checks if Error is enabled at the current root Logger.
func IsErrorEnabled() bool {
return IsLevelEnabled(level.Error)
}
// Fatal logs the provided arguments on Fatal at the current root Logger.
//
// IMPORTANT! In contrast to many other log Golang frameworks this logging Fatal
// with slf4g does not lead to an os.Exit() by default. By contract the
// application can do that but it is doing that always GRACEFUL. All processes
// should be always able to do shutdown operations if needed AND possible.
func Fatal(args ...interface{}) {
log(level.Fatal, args...)
}
// Fatalf is like Fatal but wraps the message itself in a fmt.Sprintf action.
// By contract the actual format action will not be executed before the value
// will be really consumed.
//
// IMPORTANT! In contrast to many other log Golang frameworks this logging Fatal
// with slf4g does not lead to an os.Exit() by default. By contract the
// application can do that but it is doing that always GRACEFUL. All processes
// should be always able to do shutdown operations if needed AND possible.
func Fatalf(format string, args ...interface{}) {
logf(level.Fatal, format, args...)
}
// IsFatalEnabled checks if Fatal is enabled at the current root Logger.
func IsFatalEnabled() bool {
return IsLevelEnabled(level.Fatal)
}
// With returns a root Logger which will contain the provided field.
func With(name string, value interface{}) Logger {
return GetRootLogger().With(name, value)
}
// Withf is similar to With but it adds classic fmt.Printf functions to it.
// It is defined that the format itself will not be executed before the
// consumption of the value.
func Withf(name string, format string, args ...interface{}) Logger {
return GetRootLogger().Withf(name, format, args...)
}
// WithError is similar to With but it adds specially an error field.
func WithError(err error) Logger {
return GetRootLogger().WithError(err)
}
// WithAll is similar to With but it can consume more than one field at
// once. Be aware: There is neither a guarantee that this instance will be
// copied or not.
func WithAll(of map[string]interface{}) Logger {
return GetRootLogger().WithAll(of)
}
func log(l level.Level, args ...interface{}) {
if !IsLevelEnabled(l) {
return
}
p := GetProvider()
var values map[string]interface{}
if len(args) > 0 {
values = make(map[string]interface{}, 1)
if len(args) == 1 {
values[p.GetFieldKeysSpec().GetMessage()] = args[0]
} else {
values[p.GetFieldKeysSpec().GetMessage()] = args
}
}
logger := p.GetRootLogger()
e := logger.NewEvent(l, values)
logger.Log(e, 2)
}
func logf(l level.Level, format string, args ...interface{}) {
if !IsLevelEnabled(l) {
return
}
p := GetProvider()
values := map[string]interface{}{
p.GetFieldKeysSpec().GetMessage(): fields.LazyFormat(format, args...),
}
logger := p.GetRootLogger()
e := logger.NewEvent(l, values)
logger.Log(e, 2)
}