2018-07-11 21:49:13 +00:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
2019-08-14 21:02:01 +00:00
|
|
|
"context"
|
2018-07-11 21:49:13 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2018-10-03 16:55:26 +00:00
|
|
|
"time"
|
2018-07-11 21:49:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Logger struct {
|
|
|
|
// The logs are `io.Copy`'d to this in a mutex. It's common to set this to a
|
|
|
|
// file, or leave it default which is `os.Stderr`. You can also set this to
|
2018-10-03 16:55:26 +00:00
|
|
|
// something more adventurous, such as logging to Kafka.
|
2018-07-11 21:49:13 +00:00
|
|
|
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.
|
|
|
|
Hooks LevelHooks
|
|
|
|
// 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
|
2019-04-12 15:51:37 +00:00
|
|
|
|
|
|
|
// Flag for whether to log caller info (off by default)
|
|
|
|
ReportCaller bool
|
|
|
|
|
2018-07-11 21:49:13 +00:00
|
|
|
// 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
|
|
|
|
// logged.
|
|
|
|
Level Level
|
|
|
|
// Used to sync writing to the log. Locking is enabled by Default
|
|
|
|
mu MutexWrap
|
|
|
|
// Reusable empty entry
|
|
|
|
entryPool sync.Pool
|
2018-10-15 21:36:55 +00:00
|
|
|
// Function to exit the application, defaults to `os.Exit()`
|
|
|
|
ExitFunc exitFunc
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-15 21:36:55 +00:00
|
|
|
type exitFunc func(int)
|
|
|
|
|
2018-07-11 21:49:13 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
//
|
2020-08-07 10:01:04 +00:00
|
|
|
// var log = &logrus.Logger{
|
2018-07-11 21:49:13 +00:00
|
|
|
// Out: os.Stderr,
|
2021-02-18 20:40:18 +00:00
|
|
|
// Formatter: new(logrus.JSONFormatter),
|
2020-08-07 10:01:04 +00:00
|
|
|
// Hooks: make(logrus.LevelHooks),
|
2018-07-11 21:49:13 +00:00
|
|
|
// Level: logrus.DebugLevel,
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// It's recommended to make this a global instance called `log`.
|
|
|
|
func New() *Logger {
|
|
|
|
return &Logger{
|
2019-04-12 15:51:37 +00:00
|
|
|
Out: os.Stderr,
|
|
|
|
Formatter: new(TextFormatter),
|
|
|
|
Hooks: make(LevelHooks),
|
|
|
|
Level: InfoLevel,
|
|
|
|
ExitFunc: os.Exit,
|
|
|
|
ReportCaller: false,
|
2018-07-11 21:49:13 +00: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) {
|
2018-10-03 16:55:26 +00:00
|
|
|
entry.Data = map[string]interface{}{}
|
2018-07-11 21:49:13 +00:00
|
|
|
logger.entryPool.Put(entry)
|
|
|
|
}
|
|
|
|
|
2020-08-07 10:01:04 +00:00
|
|
|
// WithField allocates a new entry and adds a field to it.
|
|
|
|
// Debug, Print, Info, Warn, Error, Fatal or Panic must be then applied to
|
|
|
|
// this new returned entry.
|
2018-07-11 21:49:13 +00:00
|
|
|
// If you want multiple fields, use `WithFields`.
|
|
|
|
func (logger *Logger) WithField(key string, value interface{}) *Entry {
|
|
|
|
entry := logger.newEntry()
|
|
|
|
defer logger.releaseEntry(entry)
|
|
|
|
return entry.WithField(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
entry := logger.newEntry()
|
|
|
|
defer logger.releaseEntry(entry)
|
|
|
|
return entry.WithFields(fields)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
entry := logger.newEntry()
|
|
|
|
defer logger.releaseEntry(entry)
|
|
|
|
return entry.WithError(err)
|
|
|
|
}
|
|
|
|
|
2019-08-14 21:02:01 +00:00
|
|
|
// Add a context to the log entry.
|
|
|
|
func (logger *Logger) WithContext(ctx context.Context) *Entry {
|
|
|
|
entry := logger.newEntry()
|
|
|
|
defer logger.releaseEntry(entry)
|
|
|
|
return entry.WithContext(ctx)
|
|
|
|
}
|
|
|
|
|
2018-10-03 16:55:26 +00:00
|
|
|
// Overrides the time of the log entry.
|
|
|
|
func (logger *Logger) WithTime(t time.Time) *Entry {
|
|
|
|
entry := logger.newEntry()
|
|
|
|
defer logger.releaseEntry(entry)
|
|
|
|
return entry.WithTime(t)
|
|
|
|
}
|
|
|
|
|
2019-08-14 21:02:01 +00:00
|
|
|
func (logger *Logger) Logf(level Level, format string, args ...interface{}) {
|
|
|
|
if logger.IsLevelEnabled(level) {
|
2018-07-11 21:49:13 +00:00
|
|
|
entry := logger.newEntry()
|
2019-08-14 21:02:01 +00:00
|
|
|
entry.Logf(level, format, args...)
|
2018-07-11 21:49:13 +00:00
|
|
|
logger.releaseEntry(entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 21:02:01 +00:00
|
|
|
func (logger *Logger) Tracef(format string, args ...interface{}) {
|
|
|
|
logger.Logf(TraceLevel, format, args...)
|
|
|
|
}
|
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
func (logger *Logger) Debugf(format string, args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Logf(DebugLevel, format, args...)
|
2019-04-12 15:51:37 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 21:49:13 +00:00
|
|
|
func (logger *Logger) Infof(format string, args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Logf(InfoLevel, format, args...)
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Printf(format string, args ...interface{}) {
|
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Printf(format, args...)
|
|
|
|
logger.releaseEntry(entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Warnf(format string, args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Logf(WarnLevel, format, args...)
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Warningf(format string, args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Warnf(format, args...)
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Errorf(format string, args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Logf(ErrorLevel, format, args...)
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Fatalf(format string, args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Logf(FatalLevel, format, args...)
|
2018-10-15 21:36:55 +00:00
|
|
|
logger.Exit(1)
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Panicf(format string, args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Logf(PanicLevel, format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Log(level Level, args ...interface{}) {
|
|
|
|
if logger.IsLevelEnabled(level) {
|
2018-07-11 21:49:13 +00:00
|
|
|
entry := logger.newEntry()
|
2019-08-14 21:02:01 +00:00
|
|
|
entry.Log(level, args...)
|
2018-07-11 21:49:13 +00:00
|
|
|
logger.releaseEntry(entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
func (logger *Logger) Trace(args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Log(TraceLevel, args...)
|
2019-04-12 15:51:37 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 21:49:13 +00:00
|
|
|
func (logger *Logger) Debug(args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Log(DebugLevel, args...)
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Info(args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Log(InfoLevel, args...)
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Print(args ...interface{}) {
|
|
|
|
entry := logger.newEntry()
|
2019-08-14 21:02:01 +00:00
|
|
|
entry.Print(args...)
|
2018-07-11 21:49:13 +00:00
|
|
|
logger.releaseEntry(entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Warn(args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Log(WarnLevel, args...)
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Warning(args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Warn(args...)
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Error(args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Log(ErrorLevel, args...)
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Fatal(args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Log(FatalLevel, args...)
|
2018-10-15 21:36:55 +00:00
|
|
|
logger.Exit(1)
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Panic(args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Log(PanicLevel, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Logln(level Level, args ...interface{}) {
|
|
|
|
if logger.IsLevelEnabled(level) {
|
2018-07-11 21:49:13 +00:00
|
|
|
entry := logger.newEntry()
|
2019-08-14 21:02:01 +00:00
|
|
|
entry.Logln(level, args...)
|
2018-07-11 21:49:13 +00:00
|
|
|
logger.releaseEntry(entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
func (logger *Logger) Traceln(args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Logln(TraceLevel, args...)
|
2019-04-12 15:51:37 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 21:49:13 +00:00
|
|
|
func (logger *Logger) Debugln(args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Logln(DebugLevel, args...)
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Infoln(args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Logln(InfoLevel, args...)
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Println(args ...interface{}) {
|
|
|
|
entry := logger.newEntry()
|
|
|
|
entry.Println(args...)
|
|
|
|
logger.releaseEntry(entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Warnln(args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Logln(WarnLevel, args...)
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Warningln(args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Warnln(args...)
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Errorln(args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Logln(ErrorLevel, args...)
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Fatalln(args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Logln(FatalLevel, args...)
|
2018-10-15 21:36:55 +00:00
|
|
|
logger.Exit(1)
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Panicln(args ...interface{}) {
|
2019-08-14 21:02:01 +00:00
|
|
|
logger.Logln(PanicLevel, args...)
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-15 21:36:55 +00:00
|
|
|
func (logger *Logger) Exit(code int) {
|
|
|
|
runHandlers()
|
|
|
|
if logger.ExitFunc == nil {
|
|
|
|
logger.ExitFunc = os.Exit
|
|
|
|
}
|
|
|
|
logger.ExitFunc(code)
|
|
|
|
}
|
|
|
|
|
2018-07-11 21:49:13 +00: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()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) level() Level {
|
|
|
|
return Level(atomic.LoadUint32((*uint32)(&logger.Level)))
|
|
|
|
}
|
|
|
|
|
2018-10-03 16:55:26 +00:00
|
|
|
// SetLevel sets the logger level.
|
2018-07-11 21:49:13 +00:00
|
|
|
func (logger *Logger) SetLevel(level Level) {
|
|
|
|
atomic.StoreUint32((*uint32)(&logger.Level), uint32(level))
|
|
|
|
}
|
|
|
|
|
2018-10-03 16:55:26 +00:00
|
|
|
// GetLevel returns the logger level.
|
|
|
|
func (logger *Logger) GetLevel() Level {
|
|
|
|
return logger.level()
|
2018-07-11 21:49:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 16:55:26 +00:00
|
|
|
// AddHook adds a hook to the logger hooks.
|
2018-07-11 21:49:13 +00:00
|
|
|
func (logger *Logger) AddHook(hook Hook) {
|
|
|
|
logger.mu.Lock()
|
|
|
|
defer logger.mu.Unlock()
|
|
|
|
logger.Hooks.Add(hook)
|
|
|
|
}
|
2018-10-03 16:55:26 +00:00
|
|
|
|
|
|
|
// IsLevelEnabled checks if the log level of the logger is greater than the level param
|
|
|
|
func (logger *Logger) IsLevelEnabled(level Level) bool {
|
|
|
|
return logger.level() >= level
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFormatter sets the logger formatter.
|
|
|
|
func (logger *Logger) SetFormatter(formatter Formatter) {
|
|
|
|
logger.mu.Lock()
|
|
|
|
defer logger.mu.Unlock()
|
|
|
|
logger.Formatter = formatter
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOutput sets the logger output.
|
|
|
|
func (logger *Logger) SetOutput(output io.Writer) {
|
|
|
|
logger.mu.Lock()
|
|
|
|
defer logger.mu.Unlock()
|
|
|
|
logger.Out = output
|
|
|
|
}
|
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
func (logger *Logger) SetReportCaller(reportCaller bool) {
|
|
|
|
logger.mu.Lock()
|
|
|
|
defer logger.mu.Unlock()
|
|
|
|
logger.ReportCaller = reportCaller
|
|
|
|
}
|
|
|
|
|
2018-10-03 16:55:26 +00:00
|
|
|
// ReplaceHooks replaces the logger hooks and returns the old ones
|
|
|
|
func (logger *Logger) ReplaceHooks(hooks LevelHooks) LevelHooks {
|
|
|
|
logger.mu.Lock()
|
|
|
|
oldHooks := logger.Hooks
|
|
|
|
logger.Hooks = hooks
|
|
|
|
logger.mu.Unlock()
|
|
|
|
return oldHooks
|
|
|
|
}
|