forked from apsdehal/go-logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
221 lines (179 loc) Β· 6.82 KB
/
log.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
// COPYRIGHT (c) 2019-2021 SILVANO ZAMPARDI, ALL RIGHTS RESERVED.
// The license for these sources can be found in the LICENSE file in the root directory of this source tree.
package log
import (
"fmt"
"io"
"os"
"runtime"
)
// Fatal is just like func l.Critical logger except that it is followed by exit to program
func (l *Logger) Fatal(message string) {
l.logInternal(LCrit, message, 2)
os.Exit(1)
}
// Fatalf is just like func l.CriticalF logger except that it is followed by exit to program
func (l *Logger) Fatalf(format string, a ...interface{}) {
l.logInternal(LCrit, fmt.Sprintf(format, a...), 2)
os.Exit(1)
}
// Panic is just like func l.Critical except that it is followed by a call to panic
func (l *Logger) Panic(message string) {
l.logInternal(LCrit, message, 2)
panic(message)
}
// Panicf is just like func l.CriticalF except that it is followed by a call to panic
func (l *Logger) Panicf(format string, a ...interface{}) {
l.logInternal(LCrit, fmt.Sprintf(format, a...), 2)
panic(fmt.Sprintf(format, a...))
}
// Critical logs a message at a Critical Level
func (l *Logger) Critical(message string) {
l.logInternal(LCrit, message, 2)
}
// Criticalf logs a message at Critical level using the same syntax and options as fmt.Printf
func (l *Logger) Criticalf(format string, a ...interface{}) {
l.logInternal(LCrit, fmt.Sprintf(format, a...), 2)
}
// Error logs a message at Error level
func (l *Logger) Error(message string) {
l.logInternal(LErr, message, 2)
}
// Errorf logs a message at Error level using the same syntax and options as fmt.Printf
func (l *Logger) Errorf(format string, a ...interface{}) {
l.logInternal(LErr, fmt.Sprintf(format, a...), 2)
}
// Warning logs a message at Warning level
func (l *Logger) Warning(message string) {
l.logInternal(LWarn, message, 2)
}
// Warningf logs a message at Warning level using the same syntax and options as fmt.Printf
func (l *Logger) Warningf(format string, a ...interface{}) {
l.logInternal(LWarn, fmt.Sprintf(format, a...), 2)
}
// Notice logs a message at Notice level
func (l *Logger) Notice(message string) {
l.logInternal(LNotice, message, 2)
}
// Noticef logs a message at Notice level using the same syntax and options as fmt.Printf
func (l *Logger) Noticef(format string, a ...interface{}) {
l.logInternal(LNotice, fmt.Sprintf(format, a...), 2)
}
// Info logs a message at Info level
func (l *Logger) Info(message string) {
l.logInternal(LInfo, message, 2)
}
// Infof logs a message at Info level using the same syntax and options as fmt.Printf
func (l *Logger) Infof(format string, a ...interface{}) {
l.logInternal(LInfo, fmt.Sprintf(format, a...), 2)
}
// Debug logs a message at Debug level
func (l *Logger) Debug(message string) {
l.logInternal(LDebug, message, 2)
}
// Debugf logs a message at Debug level using the same syntax and options as fmt.Printf
func (l *Logger) Debugf(format string, a ...interface{}) {
l.logInternal(LDebug, fmt.Sprintf(format, a...), 2)
}
// StackAsError Prints a goroutine's execution stack as an error with an optional message at the begining
func (l *Logger) StackAsError(message string) {
l.logInternal(LErr, stack(message), 2)
}
// StackAsCritical Prints a goroutine's execution stack as critical with an optional message at the begining
func (l *Logger) StackAsCritical(message string) {
l.logInternal(LCrit, stack(message), 2)
}
var defaultLogger *Logger = &Logger{
Module: "msg",
worker: newWorker("", activeFormat, activeTimeFormat, 0, true, os.Stdout, LDebug),
}
// Fatal is just like func l.Critical logger except that it is followed by exit to program
func Fatal(message string) {
defaultLogger.logInternal(LCrit, message, 2)
os.Exit(1)
}
// Fatalf is just like func l.CriticalF logger except that it is followed by exit to program
func Fatalf(format string, a ...interface{}) {
defaultLogger.logInternal(LCrit, fmt.Sprintf(format, a...), 2)
os.Exit(1)
}
// Panic is just like func l.Critical except that it is followed by a call to panic
func Panic(message string) {
defaultLogger.logInternal(LCrit, message, 2)
panic(message)
}
// Panicf is just like func l.CriticalF except that it is followed by a call to panic
func Panicf(format string, a ...interface{}) {
defaultLogger.logInternal(LCrit, fmt.Sprintf(format, a...), 2)
panic(fmt.Sprintf(format, a...))
}
// Critical logs a message at a Critical Level
func Critical(message string) {
defaultLogger.logInternal(LCrit, message, 2)
}
// Criticalf logs a message at Critical level using the same syntax and options as fmt.Printf
func Criticalf(format string, a ...interface{}) {
defaultLogger.logInternal(LCrit, fmt.Sprintf(format, a...), 2)
}
// Error logs a message at Error level
func Error(message string) {
defaultLogger.logInternal(LErr, message, 2)
}
// Errorf logs a message at Error level using the same syntax and options as fmt.Printf
func Errorf(format string, a ...interface{}) {
defaultLogger.logInternal(LErr, fmt.Sprintf(format, a...), 2)
}
// Warning logs a message at Warning level
func Warning(message string) {
defaultLogger.logInternal(LWarn, message, 2)
}
// Warningf logs a message at Warning level using the same syntax and options as fmt.Printf
func Warningf(format string, a ...interface{}) {
defaultLogger.logInternal(LWarn, fmt.Sprintf(format, a...), 2)
}
// Notice logs a message at Notice level
func Notice(message string) {
defaultLogger.logInternal(LNotice, message, 2)
}
// Noticef logs a message at Notice level using the same syntax and options as fmt.Printf
func Noticef(format string, a ...interface{}) {
defaultLogger.logInternal(LNotice, fmt.Sprintf(format, a...), 2)
}
// Info logs a message at Info level
func Info(message string) {
defaultLogger.logInternal(LInfo, message, 2)
}
// Infof logs a message at Info level using the same syntax and options as fmt.Printf
func Infof(format string, a ...interface{}) {
defaultLogger.logInternal(LInfo, fmt.Sprintf(format, a...), 2)
}
// Debug logs a message at Debug level
func Debug(message string) {
defaultLogger.logInternal(LDebug, message, 2)
}
// Debugf logs a message at Debug level using the same syntax and options as fmt.Printf
func Debugf(format string, a ...interface{}) {
defaultLogger.logInternal(LDebug, fmt.Sprintf(format, a...), 2)
}
// StackAsError Prints a goroutine's execution stack as an error with an optional message at the begining
func StackAsError(message string) {
defaultLogger.logInternal(LErr, stack(message), 2)
}
// StackAsCritical Prints a goroutine's execution stack as critical with an optional message at the begining
func StackAsCritical(message string) {
defaultLogger.logInternal(LCrit, stack(message), 2)
}
func SetOutput(w io.Writer) {
defaultLogger.SetOutput(w)
}
func SetFormat(f string) {
defaultLogger.SetFormat(f)
}
func stack(s string) string {
if s == "" {
s = "Stack info\n"
}
buf := make([]byte, 1<<16)
runtime.Stack(buf, false)
return fmt.Sprintf("%s\n%s", s, buf)
}