2017-06-24 18:25:26 +00:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Fields type, used to pass to `WithFields`.
|
|
|
|
type Fields map[string]interface{}
|
|
|
|
|
|
|
|
// Level type
|
|
|
|
type Level uint32
|
|
|
|
|
|
|
|
// Convert the Level to a string. E.g. PanicLevel becomes "panic".
|
|
|
|
func (level Level) String() string {
|
2019-01-21 16:03:35 +00:00
|
|
|
if b, err := level.MarshalText(); err == nil {
|
|
|
|
return string(b)
|
|
|
|
} else {
|
|
|
|
return "unknown"
|
2017-06-24 18:25:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseLevel takes a string level and returns the Logrus log level constant.
|
|
|
|
func ParseLevel(lvl string) (Level, error) {
|
|
|
|
switch strings.ToLower(lvl) {
|
|
|
|
case "panic":
|
|
|
|
return PanicLevel, nil
|
|
|
|
case "fatal":
|
|
|
|
return FatalLevel, nil
|
|
|
|
case "error":
|
|
|
|
return ErrorLevel, nil
|
|
|
|
case "warn", "warning":
|
|
|
|
return WarnLevel, nil
|
|
|
|
case "info":
|
|
|
|
return InfoLevel, nil
|
|
|
|
case "debug":
|
|
|
|
return DebugLevel, nil
|
2019-01-21 16:03:35 +00:00
|
|
|
case "trace":
|
|
|
|
return TraceLevel, nil
|
2017-06-24 18:25:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var l Level
|
|
|
|
return l, fmt.Errorf("not a valid logrus Level: %q", lvl)
|
|
|
|
}
|
|
|
|
|
2019-01-21 16:03:35 +00:00
|
|
|
// UnmarshalText implements encoding.TextUnmarshaler.
|
|
|
|
func (level *Level) UnmarshalText(text []byte) error {
|
|
|
|
l, err := ParseLevel(string(text))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-26 01:40:38 +00:00
|
|
|
*level = l
|
2019-01-21 16:03:35 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (level Level) MarshalText() ([]byte, error) {
|
|
|
|
switch level {
|
|
|
|
case TraceLevel:
|
|
|
|
return []byte("trace"), nil
|
|
|
|
case DebugLevel:
|
|
|
|
return []byte("debug"), nil
|
|
|
|
case InfoLevel:
|
|
|
|
return []byte("info"), nil
|
|
|
|
case WarnLevel:
|
|
|
|
return []byte("warning"), nil
|
|
|
|
case ErrorLevel:
|
|
|
|
return []byte("error"), nil
|
|
|
|
case FatalLevel:
|
|
|
|
return []byte("fatal"), nil
|
|
|
|
case PanicLevel:
|
|
|
|
return []byte("panic"), nil
|
|
|
|
}
|
|
|
|
|
2021-06-26 01:40:38 +00:00
|
|
|
return nil, fmt.Errorf("not a valid logrus level %d", level)
|
2019-01-21 16:03:35 +00:00
|
|
|
}
|
|
|
|
|
2017-06-24 18:25:26 +00:00
|
|
|
// A constant exposing all logging levels
|
|
|
|
var AllLevels = []Level{
|
|
|
|
PanicLevel,
|
|
|
|
FatalLevel,
|
|
|
|
ErrorLevel,
|
|
|
|
WarnLevel,
|
|
|
|
InfoLevel,
|
|
|
|
DebugLevel,
|
2019-01-21 16:03:35 +00:00
|
|
|
TraceLevel,
|
2017-06-24 18:25:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// These are the different logging levels. You can set the logging level to log
|
|
|
|
// on your instance of logger, obtained with `logrus.New()`.
|
|
|
|
const (
|
|
|
|
// PanicLevel level, highest level of severity. Logs and then calls panic with the
|
|
|
|
// message passed to Debug, Info, ...
|
|
|
|
PanicLevel Level = iota
|
2019-01-21 16:03:35 +00:00
|
|
|
// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
|
2017-06-24 18:25:26 +00:00
|
|
|
// logging level is set to Panic.
|
|
|
|
FatalLevel
|
|
|
|
// ErrorLevel level. Logs. Used for errors that should definitely be noted.
|
|
|
|
// Commonly used for hooks to send errors to an error tracking service.
|
|
|
|
ErrorLevel
|
|
|
|
// WarnLevel level. Non-critical entries that deserve eyes.
|
|
|
|
WarnLevel
|
|
|
|
// InfoLevel level. General operational entries about what's going on inside the
|
|
|
|
// application.
|
|
|
|
InfoLevel
|
|
|
|
// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
|
|
|
|
DebugLevel
|
2019-01-21 16:03:35 +00:00
|
|
|
// TraceLevel level. Designates finer-grained informational events than the Debug.
|
|
|
|
TraceLevel
|
2017-06-24 18:25:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Won't compile if StdLogger can't be realized by a log.Logger
|
|
|
|
var (
|
|
|
|
_ StdLogger = &log.Logger{}
|
|
|
|
_ StdLogger = &Entry{}
|
|
|
|
_ StdLogger = &Logger{}
|
|
|
|
)
|
|
|
|
|
|
|
|
// StdLogger is what your logrus-enabled library should take, that way
|
|
|
|
// it'll accept a stdlib logger and a logrus logger. There's no standard
|
|
|
|
// interface, this is the closest we get, unfortunately.
|
|
|
|
type StdLogger interface {
|
|
|
|
Print(...interface{})
|
|
|
|
Printf(string, ...interface{})
|
|
|
|
Println(...interface{})
|
|
|
|
|
|
|
|
Fatal(...interface{})
|
|
|
|
Fatalf(string, ...interface{})
|
|
|
|
Fatalln(...interface{})
|
|
|
|
|
|
|
|
Panic(...interface{})
|
|
|
|
Panicf(string, ...interface{})
|
|
|
|
Panicln(...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// The FieldLogger interface generalizes the Entry and Logger types
|
|
|
|
type FieldLogger interface {
|
|
|
|
WithField(key string, value interface{}) *Entry
|
|
|
|
WithFields(fields Fields) *Entry
|
|
|
|
WithError(err error) *Entry
|
|
|
|
|
|
|
|
Debugf(format string, args ...interface{})
|
|
|
|
Infof(format string, args ...interface{})
|
|
|
|
Printf(format string, args ...interface{})
|
|
|
|
Warnf(format string, args ...interface{})
|
|
|
|
Warningf(format string, args ...interface{})
|
|
|
|
Errorf(format string, args ...interface{})
|
|
|
|
Fatalf(format string, args ...interface{})
|
|
|
|
Panicf(format string, args ...interface{})
|
|
|
|
|
|
|
|
Debug(args ...interface{})
|
|
|
|
Info(args ...interface{})
|
|
|
|
Print(args ...interface{})
|
|
|
|
Warn(args ...interface{})
|
|
|
|
Warning(args ...interface{})
|
|
|
|
Error(args ...interface{})
|
|
|
|
Fatal(args ...interface{})
|
|
|
|
Panic(args ...interface{})
|
|
|
|
|
|
|
|
Debugln(args ...interface{})
|
|
|
|
Infoln(args ...interface{})
|
|
|
|
Println(args ...interface{})
|
|
|
|
Warnln(args ...interface{})
|
|
|
|
Warningln(args ...interface{})
|
|
|
|
Errorln(args ...interface{})
|
|
|
|
Fatalln(args ...interface{})
|
|
|
|
Panicln(args ...interface{})
|
2019-01-21 16:03:35 +00:00
|
|
|
|
|
|
|
// IsDebugEnabled() bool
|
|
|
|
// IsInfoEnabled() bool
|
|
|
|
// IsWarnEnabled() bool
|
|
|
|
// IsErrorEnabled() bool
|
|
|
|
// IsFatalEnabled() bool
|
|
|
|
// IsPanicEnabled() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ext1FieldLogger (the first extension to FieldLogger) is superfluous, it is
|
|
|
|
// here for consistancy. Do not use. Use Logger or Entry instead.
|
|
|
|
type Ext1FieldLogger interface {
|
|
|
|
FieldLogger
|
|
|
|
Tracef(format string, args ...interface{})
|
|
|
|
Trace(args ...interface{})
|
|
|
|
Traceln(args ...interface{})
|
2017-06-24 18:25:26 +00:00
|
|
|
}
|