2015-05-22 20:37:40 +02:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Logger struct {
|
|
|
|
// The logs are `io.Copy`'d to this in a mutex. It's common to set this to a
|
2016-04-12 00:55:13 +02:00
|
|
|
// file, or leave it default which is `os.Stderr`. You can also set this to
|
2015-05-22 20:37:40 +02: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-04-12 00:55:13 +02:00
|
|
|
Hooks LevelHooks
|
2015-05-22 20:37:40 +02: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
|
|
|
|
// logged. `logrus.Debug` is useful in
|
|
|
|
Level Level
|
|
|
|
// Used to sync writing to the log.
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-04-12 00:55:13 +02:00
|
|
|
// Hooks: make(LevelHooks),
|
2015-05-22 20:37:40 +02:00
|
|
|
// Level: logrus.DebugLevel,
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// It's recommended to make this a global instance called `log`.
|
|
|
|
func New() *Logger {
|
|
|
|
return &Logger{
|
2016-04-12 00:55:13 +02:00
|
|
|
Out: os.Stderr,
|
2015-05-22 20:37:40 +02:00
|
|
|
Formatter: new(TextFormatter),
|
2016-04-12 00:55:13 +02:00
|
|
|
Hooks: make(LevelHooks),
|
2015-05-22 20:37:40 +02:00
|
|
|
Level: InfoLevel,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a field to the log entry, note that you it doesn't log until you call
|
|
|
|
// Debug, Print, Info, Warn, Fatal or Panic. It only creates a log entry.
|
2016-04-12 00:55:13 +02:00
|
|
|
// If you want multiple fields, use `WithFields`.
|
2015-05-22 20:37:40 +02:00
|
|
|
func (logger *Logger) WithField(key string, value interface{}) *Entry {
|
|
|
|
return NewEntry(logger).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 {
|
|
|
|
return NewEntry(logger).WithFields(fields)
|
|
|
|
}
|
|
|
|
|
2016-04-12 00:55:13 +02: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 {
|
|
|
|
return NewEntry(logger).WithError(err)
|
|
|
|
}
|
|
|
|
|
2015-05-22 20:37:40 +02:00
|
|
|
func (logger *Logger) Debugf(format string, args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= DebugLevel {
|
|
|
|
NewEntry(logger).Debugf(format, args...)
|
|
|
|
}
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Infof(format string, args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= InfoLevel {
|
|
|
|
NewEntry(logger).Infof(format, args...)
|
|
|
|
}
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Printf(format string, args ...interface{}) {
|
|
|
|
NewEntry(logger).Printf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Warnf(format string, args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= WarnLevel {
|
|
|
|
NewEntry(logger).Warnf(format, args...)
|
|
|
|
}
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Warningf(format string, args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= WarnLevel {
|
|
|
|
NewEntry(logger).Warnf(format, args...)
|
|
|
|
}
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Errorf(format string, args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= ErrorLevel {
|
|
|
|
NewEntry(logger).Errorf(format, args...)
|
|
|
|
}
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Fatalf(format string, args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= FatalLevel {
|
|
|
|
NewEntry(logger).Fatalf(format, args...)
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Panicf(format string, args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= PanicLevel {
|
|
|
|
NewEntry(logger).Panicf(format, args...)
|
|
|
|
}
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Debug(args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= DebugLevel {
|
|
|
|
NewEntry(logger).Debug(args...)
|
|
|
|
}
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Info(args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= InfoLevel {
|
|
|
|
NewEntry(logger).Info(args...)
|
|
|
|
}
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Print(args ...interface{}) {
|
|
|
|
NewEntry(logger).Info(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Warn(args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= WarnLevel {
|
|
|
|
NewEntry(logger).Warn(args...)
|
|
|
|
}
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Warning(args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= WarnLevel {
|
|
|
|
NewEntry(logger).Warn(args...)
|
|
|
|
}
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Error(args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= ErrorLevel {
|
|
|
|
NewEntry(logger).Error(args...)
|
|
|
|
}
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Fatal(args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= FatalLevel {
|
|
|
|
NewEntry(logger).Fatal(args...)
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Panic(args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= PanicLevel {
|
|
|
|
NewEntry(logger).Panic(args...)
|
|
|
|
}
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Debugln(args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= DebugLevel {
|
|
|
|
NewEntry(logger).Debugln(args...)
|
|
|
|
}
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Infoln(args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= InfoLevel {
|
|
|
|
NewEntry(logger).Infoln(args...)
|
|
|
|
}
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Println(args ...interface{}) {
|
|
|
|
NewEntry(logger).Println(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Warnln(args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= WarnLevel {
|
|
|
|
NewEntry(logger).Warnln(args...)
|
|
|
|
}
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Warningln(args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= WarnLevel {
|
|
|
|
NewEntry(logger).Warnln(args...)
|
|
|
|
}
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Errorln(args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= ErrorLevel {
|
|
|
|
NewEntry(logger).Errorln(args...)
|
|
|
|
}
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Fatalln(args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= FatalLevel {
|
|
|
|
NewEntry(logger).Fatalln(args...)
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (logger *Logger) Panicln(args ...interface{}) {
|
2016-04-12 00:55:13 +02:00
|
|
|
if logger.Level >= PanicLevel {
|
|
|
|
NewEntry(logger).Panicln(args...)
|
|
|
|
}
|
2015-05-22 20:37:40 +02:00
|
|
|
}
|