blob: b769f3d3522c1baa000a02b0c9763b8356f23e75 [file] [log] [blame]
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -08001package logrus
2
3import (
4 "io"
5 "os"
6 "sync"
7)
8
9type Logger struct {
10 // The logs are `io.Copy`'d to this in a mutex. It's common to set this to a
11 // file, or leave it default which is `os.Stderr`. You can also set this to
12 // something more adventorous, such as logging to Kafka.
13 Out io.Writer
14 // Hooks for the logger instance. These allow firing events based on logging
15 // levels and log entries. For example, to send errors to an error tracking
16 // service, log to StatsD or dump the core on fatal errors.
17 Hooks LevelHooks
18 // All log entries pass through the formatter before logged to Out. The
19 // included formatters are `TextFormatter` and `JSONFormatter` for which
20 // TextFormatter is the default. In development (when a TTY is attached) it
21 // logs with colors, but to a file it wouldn't. You can easily implement your
22 // own that implements the `Formatter` interface, see the `README` or included
23 // formatters for examples.
24 Formatter Formatter
25 // The logging level the logger should log at. This is typically (and defaults
26 // to) `logrus.Info`, which allows Info(), Warn(), Error() and Fatal() to be
27 // logged. `logrus.Debug` is useful in
28 Level Level
David K. Bainbridge528b3182017-01-23 08:51:59 -080029 // Used to sync writing to the log. Locking is enabled by Default
30 mu MutexWrap
31 // Reusable empty entry
32 entryPool sync.Pool
33}
34
35type MutexWrap struct {
36 lock sync.Mutex
37 disabled bool
38}
39
40func (mw *MutexWrap) Lock() {
41 if !mw.disabled {
42 mw.lock.Lock()
43 }
44}
45
46func (mw *MutexWrap) Unlock() {
47 if !mw.disabled {
48 mw.lock.Unlock()
49 }
50}
51
52func (mw *MutexWrap) Disable() {
53 mw.disabled = true
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -080054}
55
56// Creates a new logger. Configuration should be set by changing `Formatter`,
57// `Out` and `Hooks` directly on the default logger instance. You can also just
58// instantiate your own:
59//
60// var log = &Logger{
61// Out: os.Stderr,
62// Formatter: new(JSONFormatter),
63// Hooks: make(LevelHooks),
64// Level: logrus.DebugLevel,
65// }
66//
67// It's recommended to make this a global instance called `log`.
68func New() *Logger {
69 return &Logger{
70 Out: os.Stderr,
71 Formatter: new(TextFormatter),
72 Hooks: make(LevelHooks),
73 Level: InfoLevel,
74 }
75}
76
David K. Bainbridge528b3182017-01-23 08:51:59 -080077func (logger *Logger) newEntry() *Entry {
78 entry, ok := logger.entryPool.Get().(*Entry)
79 if ok {
80 return entry
81 }
82 return NewEntry(logger)
83}
84
85func (logger *Logger) releaseEntry(entry *Entry) {
86 logger.entryPool.Put(entry)
87}
88
89// Adds a field to the log entry, note that it doesn't log until you call
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -080090// Debug, Print, Info, Warn, Fatal or Panic. It only creates a log entry.
91// If you want multiple fields, use `WithFields`.
92func (logger *Logger) WithField(key string, value interface{}) *Entry {
David K. Bainbridge528b3182017-01-23 08:51:59 -080093 entry := logger.newEntry()
94 defer logger.releaseEntry(entry)
95 return entry.WithField(key, value)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -080096}
97
98// Adds a struct of fields to the log entry. All it does is call `WithField` for
99// each `Field`.
100func (logger *Logger) WithFields(fields Fields) *Entry {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800101 entry := logger.newEntry()
102 defer logger.releaseEntry(entry)
103 return entry.WithFields(fields)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800104}
105
106// Add an error as single field to the log entry. All it does is call
107// `WithError` for the given `error`.
108func (logger *Logger) WithError(err error) *Entry {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800109 entry := logger.newEntry()
110 defer logger.releaseEntry(entry)
111 return entry.WithError(err)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800112}
113
114func (logger *Logger) Debugf(format string, args ...interface{}) {
115 if logger.Level >= DebugLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800116 entry := logger.newEntry()
117 entry.Debugf(format, args...)
118 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800119 }
120}
121
122func (logger *Logger) Infof(format string, args ...interface{}) {
123 if logger.Level >= InfoLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800124 entry := logger.newEntry()
125 entry.Infof(format, args...)
126 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800127 }
128}
129
130func (logger *Logger) Printf(format string, args ...interface{}) {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800131 entry := logger.newEntry()
132 entry.Printf(format, args...)
133 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800134}
135
136func (logger *Logger) Warnf(format string, args ...interface{}) {
137 if logger.Level >= WarnLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800138 entry := logger.newEntry()
139 entry.Warnf(format, args...)
140 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800141 }
142}
143
144func (logger *Logger) Warningf(format string, args ...interface{}) {
145 if logger.Level >= WarnLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800146 entry := logger.newEntry()
147 entry.Warnf(format, args...)
148 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800149 }
150}
151
152func (logger *Logger) Errorf(format string, args ...interface{}) {
153 if logger.Level >= ErrorLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800154 entry := logger.newEntry()
155 entry.Errorf(format, args...)
156 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800157 }
158}
159
160func (logger *Logger) Fatalf(format string, args ...interface{}) {
161 if logger.Level >= FatalLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800162 entry := logger.newEntry()
163 entry.Fatalf(format, args...)
164 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800165 }
David K. Bainbridge528b3182017-01-23 08:51:59 -0800166 Exit(1)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800167}
168
169func (logger *Logger) Panicf(format string, args ...interface{}) {
170 if logger.Level >= PanicLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800171 entry := logger.newEntry()
172 entry.Panicf(format, args...)
173 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800174 }
175}
176
177func (logger *Logger) Debug(args ...interface{}) {
178 if logger.Level >= DebugLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800179 entry := logger.newEntry()
180 entry.Debug(args...)
181 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800182 }
183}
184
185func (logger *Logger) Info(args ...interface{}) {
186 if logger.Level >= InfoLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800187 entry := logger.newEntry()
188 entry.Info(args...)
189 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800190 }
191}
192
193func (logger *Logger) Print(args ...interface{}) {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800194 entry := logger.newEntry()
195 entry.Info(args...)
196 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800197}
198
199func (logger *Logger) Warn(args ...interface{}) {
200 if logger.Level >= WarnLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800201 entry := logger.newEntry()
202 entry.Warn(args...)
203 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800204 }
205}
206
207func (logger *Logger) Warning(args ...interface{}) {
208 if logger.Level >= WarnLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800209 entry := logger.newEntry()
210 entry.Warn(args...)
211 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800212 }
213}
214
215func (logger *Logger) Error(args ...interface{}) {
216 if logger.Level >= ErrorLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800217 entry := logger.newEntry()
218 entry.Error(args...)
219 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800220 }
221}
222
223func (logger *Logger) Fatal(args ...interface{}) {
224 if logger.Level >= FatalLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800225 entry := logger.newEntry()
226 entry.Fatal(args...)
227 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800228 }
David K. Bainbridge528b3182017-01-23 08:51:59 -0800229 Exit(1)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800230}
231
232func (logger *Logger) Panic(args ...interface{}) {
233 if logger.Level >= PanicLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800234 entry := logger.newEntry()
235 entry.Panic(args...)
236 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800237 }
238}
239
240func (logger *Logger) Debugln(args ...interface{}) {
241 if logger.Level >= DebugLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800242 entry := logger.newEntry()
243 entry.Debugln(args...)
244 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800245 }
246}
247
248func (logger *Logger) Infoln(args ...interface{}) {
249 if logger.Level >= InfoLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800250 entry := logger.newEntry()
251 entry.Infoln(args...)
252 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800253 }
254}
255
256func (logger *Logger) Println(args ...interface{}) {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800257 entry := logger.newEntry()
258 entry.Println(args...)
259 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800260}
261
262func (logger *Logger) Warnln(args ...interface{}) {
263 if logger.Level >= WarnLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800264 entry := logger.newEntry()
265 entry.Warnln(args...)
266 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800267 }
268}
269
270func (logger *Logger) Warningln(args ...interface{}) {
271 if logger.Level >= WarnLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800272 entry := logger.newEntry()
273 entry.Warnln(args...)
274 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800275 }
276}
277
278func (logger *Logger) Errorln(args ...interface{}) {
279 if logger.Level >= ErrorLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800280 entry := logger.newEntry()
281 entry.Errorln(args...)
282 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800283 }
284}
285
286func (logger *Logger) Fatalln(args ...interface{}) {
287 if logger.Level >= FatalLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800288 entry := logger.newEntry()
289 entry.Fatalln(args...)
290 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800291 }
David K. Bainbridge528b3182017-01-23 08:51:59 -0800292 Exit(1)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800293}
294
295func (logger *Logger) Panicln(args ...interface{}) {
296 if logger.Level >= PanicLevel {
David K. Bainbridge528b3182017-01-23 08:51:59 -0800297 entry := logger.newEntry()
298 entry.Panicln(args...)
299 logger.releaseEntry(entry)
David K. Bainbridgeee02c8e2016-11-09 09:13:46 -0800300 }
301}
David K. Bainbridge528b3182017-01-23 08:51:59 -0800302
303//When file is opened with appending mode, it's safe to
304//write concurrently to a file (within 4k message on Linux).
305//In these cases user can choose to disable the lock.
306func (logger *Logger) SetNoLock() {
307 logger.mu.Disable()
308}