-
Notifications
You must be signed in to change notification settings - Fork 0
/
logng.go
258 lines (213 loc) · 8.18 KB
/
logng.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
// Package logng provides structured and leveled logging.
package logng
import (
"io"
"os"
"time"
)
// Reset resets the default Logger and the default TextOutput.
func Reset() {
SetOutput(defaultTextOutput)
SetSeverity(SeverityInfo)
SetVerbose(0)
SetPrintSeverity(SeverityInfo)
SetStackTraceSeverity(SeverityNone)
SetStackTraceSize(64)
SetTextOutputWriter(defaultTextOutputWriter)
SetTextOutputFlags(TextOutputFlagDefault)
}
var (
defaultLogger = NewLogger(defaultTextOutput, SeverityInfo, 0)
)
// DefaultLogger returns the default Logger.
func DefaultLogger() *Logger {
return defaultLogger
}
// Clone clones the default Logger.
func Clone() *Logger {
return defaultLogger.Clone()
}
// Fatal logs to the FATAL severity logs to the default Logger, then calls os.Exit(1).
func Fatal(args ...interface{}) {
defaultLogger.log(SeverityFatal, args...)
os.Exit(1)
}
// Fatalf logs to the FATAL severity logs to the default Logger, then calls os.Exit(1).
func Fatalf(format string, args ...interface{}) {
defaultLogger.logf(SeverityFatal, format, args...)
os.Exit(1)
}
// Fatalln logs to the FATAL severity logs to the default Logger, then calls os.Exit(1).
func Fatalln(args ...interface{}) {
defaultLogger.logln(SeverityFatal, args...)
os.Exit(1)
}
// Error logs to the ERROR severity logs to the default Logger.
func Error(args ...interface{}) {
defaultLogger.log(SeverityError, args...)
}
// Errorf logs to the ERROR severity logs to the default Logger.
func Errorf(format string, args ...interface{}) {
defaultLogger.logf(SeverityError, format, args...)
}
// Errorln logs to the ERROR severity logs to the default Logger.
func Errorln(args ...interface{}) {
defaultLogger.logln(SeverityError, args...)
}
// Warning logs to the WARNING severity logs to the default Logger.
func Warning(args ...interface{}) {
defaultLogger.log(SeverityWarning, args...)
}
// Warningf logs to the WARNING severity logs to the default Logger.
func Warningf(format string, args ...interface{}) {
defaultLogger.logf(SeverityWarning, format, args...)
}
// Warningln logs to the WARNING severity logs to the default Logger.
func Warningln(args ...interface{}) {
defaultLogger.logln(SeverityWarning, args...)
}
// Info logs to the INFO severity logs to the default Logger.
func Info(args ...interface{}) {
defaultLogger.log(SeverityInfo, args...)
}
// Infof logs to the INFO severity logs to the default Logger.
func Infof(format string, args ...interface{}) {
defaultLogger.logf(SeverityInfo, format, args...)
}
// Infoln logs to the INFO severity logs to the default Logger.
func Infoln(args ...interface{}) {
defaultLogger.logln(SeverityInfo, args...)
}
// Debug logs to the DEBUG severity logs to the default Logger.
func Debug(args ...interface{}) {
defaultLogger.log(SeverityDebug, args...)
}
// Debugf logs to the DEBUG severity logs to the default Logger.
func Debugf(format string, args ...interface{}) {
defaultLogger.logf(SeverityDebug, format, args...)
}
// Debugln logs to the DEBUG severity logs to the default Logger.
func Debugln(args ...interface{}) {
defaultLogger.logln(SeverityDebug, args...)
}
// Print logs a log which has the default Logger's print severity to the default Logger.
func Print(args ...interface{}) {
defaultLogger.log(severityPrint, args...)
}
// Printf logs a log which has the default Logger's print severity to the default Logger.
func Printf(format string, args ...interface{}) {
defaultLogger.logf(severityPrint, format, args...)
}
// Println logs a log which has the default Logger's print severity to the default Logger.
func Println(args ...interface{}) {
defaultLogger.logln(severityPrint, args...)
}
// SetOutput sets the default Logger's output.
// It returns the default Logger.
// By default, the default TextOutput.
func SetOutput(output Output) *Logger {
return defaultLogger.SetOutput(output)
}
// SetSeverity sets the default Logger's severity.
// If severity is invalid, it sets SeverityInfo.
// It returns the default Logger.
// By default, SeverityInfo.
func SetSeverity(severity Severity) *Logger {
return defaultLogger.SetSeverity(severity)
}
// SetVerbose sets the default Logger's verbose.
// It returns the default Logger.
// By default, 0.
func SetVerbose(verbose Verbose) *Logger {
return defaultLogger.SetVerbose(verbose)
}
// SetPrintSeverity sets the default Logger's severity level which is using with Print methods.
// If printSeverity is invalid, it sets SeverityInfo.
// It returns the default Logger.
// By default, SeverityInfo.
func SetPrintSeverity(printSeverity Severity) *Logger {
return defaultLogger.SetPrintSeverity(printSeverity)
}
// SetStackTraceSeverity sets the default Logger's severity level which saves stack trace into Log.
// If stackTraceSeverity is invalid, it sets SeverityNone.
// It returns the default Logger.
// By default, SeverityNone.
func SetStackTraceSeverity(stackTraceSeverity Severity) *Logger {
return defaultLogger.SetStackTraceSeverity(stackTraceSeverity)
}
// SetStackTraceSize sets the maximum program counter size of the stack trace for the default Logger.
// If stackTraceSize is out of range, it sets 64. The range is 1 to 16384 each included.
// It returns the default Logger.
// By default, 64.
func SetStackTraceSize(stackTraceSize int) *Logger {
return defaultLogger.SetStackTraceSize(stackTraceSize)
}
// V clones the default Logger with the given verbosity if the default Logger's verbose is greater or equal to the given verbosity, otherwise returns nil.
func V(verbosity Verbose) *Logger {
return defaultLogger.V(verbosity)
}
// WithVerbosity clones the default Logger with the given verbosity.
func WithVerbosity(verbosity Verbose) *Logger {
return defaultLogger.WithVerbosity(verbosity)
}
// WithTime clones the default Logger with the given time.
func WithTime(tm time.Time) *Logger {
return defaultLogger.WithTime(tm)
}
// WithoutTime clones the default Logger without time.
func WithoutTime() *Logger {
return defaultLogger.WithoutTime()
}
// WithPrefix clones the default Logger and adds the given prefix to the end of the underlying prefix.
func WithPrefix(args ...interface{}) *Logger {
return defaultLogger.WithPrefix(args...)
}
// WithPrefixf clones the default Logger and adds the given prefix to the end of the underlying prefix.
func WithPrefixf(format string, args ...interface{}) *Logger {
return defaultLogger.WithPrefixf(format, args...)
}
// WithSuffix clones the default Logger and adds the given suffix to the beginning of the underlying suffix.
func WithSuffix(args ...interface{}) *Logger {
return defaultLogger.WithSuffix(args...)
}
// WithSuffixf clones the default Logger and adds the given suffix to the beginning of the underlying suffix.
func WithSuffixf(format string, args ...interface{}) *Logger {
return defaultLogger.WithSuffixf(format, args...)
}
// WithFields clones the default Logger with given fields.
func WithFields(fields ...Field) *Logger {
return defaultLogger.WithFields(fields...)
}
// WithFieldKeyVals clones the default Logger with given keys and values of Field.
func WithFieldKeyVals(kvs ...interface{}) *Logger {
return defaultLogger.WithFieldKeyVals(kvs...)
}
// WithFieldMap clones the default Logger with the given field map.
func WithFieldMap(fieldMap map[string]interface{}) *Logger {
return defaultLogger.WithFieldMap(fieldMap)
}
// WithCtxErrVerbosity clones the default Logger with context error verbosity.
// If the log has an error and the error is an context error, the given value is used as verbosity.
func WithCtxErrVerbosity(verbosity Verbose) *Logger {
return defaultLogger.WithCtxErrVerbosity(verbosity)
}
var (
defaultTextOutput = NewTextOutput(defaultTextOutputWriter, TextOutputFlagDefault)
defaultTextOutputWriter = os.Stderr
)
// DefaultTextOutput returns the default TextOutput.
func DefaultTextOutput() *TextOutput {
return defaultTextOutput
}
// SetTextOutputWriter sets the default TextOutput's writer.
// It returns the default TextOutput.
// By default, os.Stderr.
func SetTextOutputWriter(w io.Writer) *TextOutput {
return defaultTextOutput.SetWriter(w)
}
// SetTextOutputFlags sets the default TextOutput's flags.
// It returns the default TextOutput.
// By default, TextOutputFlagDefault.
func SetTextOutputFlags(flags TextOutputFlag) *TextOutput {
return defaultTextOutput.SetFlags(flags)
}