2014-10-24 12:38:36 -04:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"sync"
|
2017-07-26 17:42:13 -04:00
|
|
|
"sync/atomic"
|
2014-10-24 12:38:36 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Logger struct {
|
|
|
|
// The logs are `io.Copy`'d to this in a mutex. It's common to set this to a
|
2016-01-20 12:47:41 -05:00
|
|
|
// file, or leave it default which is `os.Stderr`. You can also set this to
|
2014-10-24 12:38:36 -04:00
|
|
|
// something more adventorous, such as logging to Kafka.
|
|
|
|
Out io.Writer
|
|
|
|
// Hooks for the logger instance. These allow firing events based on logging
|
|
|
|
// levels and log entries. For example, to send errors to an error tracking
|
|
|
|
// service, log to StatsD or dump the core on fatal errors.
|
2016-01-20 12:47:41 -05:00
|
|
|
Hooks LevelHooks
|
2014-10-24 12:38:36 -04:00
|
|
|
// All log entries pass through the formatter before logged to Out. The
|
|
|
|
// included formatters are `TextFormatter` and `JSONFormatter` for which
|
|
|
|
// TextFormatter is the default. In development (when a TTY is attached) it
|
|
|
|
// logs with colors, but to a file it wouldn't. You can easily implement your
|
|
|
|
// own that implements the `Formatter` interface, see the `README` or included
|
|
|
|
// formatters for examples.
|
|
|
|
Formatter Formatter
|
|
|
|
// The logging level the logger should log at. This is typically (and defaults
|
|
|
|
// to) `logrus.Info`, which allows Info(), Warn(), Error() and Fatal() to be
|
2017-08-08 13:39:55 -04:00
|
|
|
// logged.
|
2014-10-24 12:38:36 -04:00
|
|
|
Level Level
|
2016-09-28 10:27:36 -04:00
|
|
|
// Used to sync writing to the log. Locking is enabled by Default
|
|
|
|
mu MutexWrap
|
|
|
|
// Reusable empty entry
|
|
|
|
entryPool sync.Pool
|
|
|
|
}
|
|
|
|
|
|
|
|
type MutexWrap struct {
|
|
|
|
lock sync.Mutex
|
|
|
|
disabled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mw *MutexWrap) Lock() {
|
|
|
|
if !mw.disabled {
|
|
|
|
mw.lock.Lock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mw *MutexWrap) Unlock() {
|
|
|
|
if !mw.disabled {
|
|
|
|
mw.lock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mw *MutexWrap) Disable() {
|
|
|
|
mw.disabled = true
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new logger. Configuration should be set by changing `Formatter`,
|
|
|
|
// `Out` and `Hooks` directly on the default logger instance. You can also just
|
|
|
|
// instantiate your own:
|
|
|
|
//
|
|
|
|
// var log = &Logger{
|
|
|
|
// Out: os.Stderr,
|
|
|
|
// Formatter: new(JSONFormatter),
|
2016-01-20 12:47:41 -05:00
|
|
|
// Hooks: make(LevelHooks),
|
2015-03-11 13:43:56 -04:00
|
|
|
// Level: logrus.DebugLevel,
|
2014-10-24 12:38:36 -04:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// It's recommended to make this a global instance called `log`.
|
|
|
|
func New() *Logger {
|
|
|
|
return &Logger{
|
2015-06-03 16:21:58 -04:00
|
|
|
Out: os.Stderr,
|
2014-10-24 12:38:36 -04:00
|
|
|
Formatter: new(TextFormatter),
|
2016-01-20 12:47:41 -05:00
|
|
|
Hooks: make(LevelHooks),
|
2014-10-24 12:38:36 -04:00
|
|
|
Level: InfoLevel,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 10:27:36 -04:00
|
|
|
func (logger *Logger) newEntry() *Entry {
|
|
|
|
entry, ok := logger.entryPool.Get().(*Entry)
|
|
|
|
if ok {
|
|
|
|
return entry
|
|
|
|
}
|
|
|
|
return NewEntry(logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) releaseEntry(entry *Entry) {
|
|
|
|
logger.entryPool.Put(entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a field to the log entry, note that it doesn't log until you call
|
2014-10-24 12:38:36 -04:00
|
|
|
// Debug, Print, Info, Warn, Fatal or Panic. It only creates a log entry.
|
2016-01-20 12:47:41 -05:00
|
|
|
// If you want multiple fields, use `WithFields`.
|
2014-10-24 12:38:36 -04:00
|
|
|
func (logger *Logger) WithField(key string, value interface{}) *Entry {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
defer logger.releaseEntry(entry)
|
|
|
|
return entry.WithField(key, value)
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a struct of fields to the log entry. All it does is call `WithField` for
|
|
|
|
// each `Field`.
|
|
|
|
func (logger *Logger) WithFields(fields Fields) *Entry {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
defer logger.releaseEntry(entry)
|
|
|
|
return entry.WithFields(fields)
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
2016-02-02 20:42:19 -05:00
|
|
|
// Add an error as single field to the log entry. All it does is call
|
|
|
|
// `WithError` for the given `error`.
|
|
|
|
func (logger *Logger) WithError(err error) *Entry {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
defer logger.releaseEntry(entry)
|
|
|
|
return entry.WithError(err)
|
2016-02-02 20:42:19 -05:00
|
|
|
}
|
|
|
|
|
2014-10-24 12:38:36 -04:00
|
|
|
func (logger *Logger) Debugf(format string, args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= DebugLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Debugf(format, args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Infof(format string, args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= InfoLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Infof(format, args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Printf(format string, args ...interface{}) {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Printf(format, args...)
|
|
|
|
logger.releaseEntry(entry)
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Warnf(format string, args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= WarnLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Warnf(format, args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Warningf(format string, args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= WarnLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Warnf(format, args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Errorf(format string, args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= ErrorLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Errorf(format, args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Fatalf(format string, args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= FatalLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Fatalf(format, args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2016-09-28 10:27:36 -04:00
|
|
|
Exit(1)
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Panicf(format string, args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= PanicLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Panicf(format, args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Debug(args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= DebugLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Debug(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Info(args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= InfoLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Info(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Print(args ...interface{}) {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Info(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Warn(args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= WarnLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Warn(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Warning(args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= WarnLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Warn(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Error(args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= ErrorLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Error(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Fatal(args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= FatalLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Fatal(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2016-09-28 10:27:36 -04:00
|
|
|
Exit(1)
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Panic(args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= PanicLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Panic(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Debugln(args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= DebugLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Debugln(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Infoln(args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= InfoLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Infoln(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Println(args ...interface{}) {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Println(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Warnln(args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= WarnLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Warnln(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Warningln(args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= WarnLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Warnln(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Errorln(args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= ErrorLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Errorln(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Fatalln(args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= FatalLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Fatalln(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2016-09-28 10:27:36 -04:00
|
|
|
Exit(1)
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Panicln(args ...interface{}) {
|
2017-07-26 17:42:13 -04:00
|
|
|
if logger.level() >= PanicLevel {
|
2016-09-28 10:27:36 -04:00
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Panicln(args...)
|
|
|
|
logger.releaseEntry(entry)
|
2015-03-21 22:06:50 -04:00
|
|
|
}
|
2014-10-24 12:38:36 -04:00
|
|
|
}
|
2016-09-28 10:27:36 -04:00
|
|
|
|
|
|
|
//When file is opened with appending mode, it's safe to
|
|
|
|
//write concurrently to a file (within 4k message on Linux).
|
|
|
|
//In these cases user can choose to disable the lock.
|
|
|
|
func (logger *Logger) SetNoLock() {
|
|
|
|
logger.mu.Disable()
|
|
|
|
}
|
2017-07-26 17:42:13 -04:00
|
|
|
|
|
|
|
func (logger *Logger) level() Level {
|
|
|
|
return Level(atomic.LoadUint32((*uint32)(&logger.Level)))
|
|
|
|
}
|
|
|
|
|
2017-08-08 13:39:55 -04:00
|
|
|
func (logger *Logger) SetLevel(level Level) {
|
2017-07-26 17:42:13 -04:00
|
|
|
atomic.StoreUint32((*uint32)(&logger.Level), uint32(level))
|
|
|
|
}
|