2014-02-24 04:50:42 +04:00
|
|
|
package logrus
|
2014-02-23 18:57:04 +04:00
|
|
|
|
2014-03-14 23:07:57 +04:00
|
|
|
import (
|
2014-09-18 02:03:34 +04:00
|
|
|
"fmt"
|
2014-03-14 23:07:57 +04:00
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2014-03-12 18:34:29 +04:00
|
|
|
// Fields type, used to pass to `WithFields`.
|
2014-03-11 03:22:08 +04:00
|
|
|
type Fields map[string]interface{}
|
2014-02-23 18:57:04 +04:00
|
|
|
|
2014-03-12 18:34:29 +04:00
|
|
|
// Level type
|
2014-03-11 03:52:39 +04:00
|
|
|
type Level uint8
|
2014-02-23 18:57:04 +04:00
|
|
|
|
2014-07-27 05:02:08 +04:00
|
|
|
// Convert the Level to a string. E.g. PanicLevel becomes "panic".
|
|
|
|
func (level Level) String() string {
|
|
|
|
switch level {
|
|
|
|
case DebugLevel:
|
|
|
|
return "debug"
|
|
|
|
case InfoLevel:
|
|
|
|
return "info"
|
|
|
|
case WarnLevel:
|
|
|
|
return "warning"
|
|
|
|
case ErrorLevel:
|
|
|
|
return "error"
|
|
|
|
case FatalLevel:
|
|
|
|
return "fatal"
|
|
|
|
case PanicLevel:
|
|
|
|
return "panic"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
|
2015-09-12 23:41:06 +03:00
|
|
|
// UnmarshalText decodes text to the level.
|
|
|
|
func (level *Level) UnmarshalText(text []byte) error {
|
|
|
|
if len(text) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
parsed, err := ParseLevel(string(text))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*level = parsed
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalText encodes the level into UTF-8-encoded text and returns the result.
|
|
|
|
func (level Level) MarshalText() (text []byte, err error) {
|
|
|
|
return []byte(level.String()), nil
|
|
|
|
}
|
|
|
|
|
2014-09-25 07:10:06 +04:00
|
|
|
// ParseLevel takes a string level and returns the Logrus log level constant.
|
2014-09-18 02:03:34 +04:00
|
|
|
func ParseLevel(lvl string) (Level, error) {
|
|
|
|
switch 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
|
2014-09-25 07:10:06 +04:00
|
|
|
case "debug":
|
2014-09-18 02:03:34 +04:00
|
|
|
return DebugLevel, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var l Level
|
|
|
|
return l, fmt.Errorf("not a valid logrus Level: %q", lvl)
|
|
|
|
}
|
|
|
|
|
2014-03-12 18:34:29 +04:00
|
|
|
// These are the different logging levels. You can set the logging level to log
|
|
|
|
// on your instance of logger, obtained with `logrus.New()`.
|
2014-02-23 18:57:04 +04:00
|
|
|
const (
|
2014-07-16 02:30:01 +04:00
|
|
|
// PanicLevel level, highest level of severity. Logs and then calls panic with the
|
2014-03-12 18:34:29 +04:00
|
|
|
// message passed to Debug, Info, ...
|
2014-07-16 02:30:01 +04:00
|
|
|
PanicLevel Level = iota
|
|
|
|
// FatalLevel level. Logs and then calls `os.Exit(1)`. It will exit even if the
|
2014-03-12 18:34:29 +04:00
|
|
|
// logging level is set to Panic.
|
2014-07-16 02:30:01 +04:00
|
|
|
FatalLevel
|
|
|
|
// ErrorLevel level. Logs. Used for errors that should definitely be noted.
|
2014-03-12 18:34:29 +04:00
|
|
|
// Commonly used for hooks to send errors to an error tracking service.
|
2014-07-16 02:30:01 +04:00
|
|
|
ErrorLevel
|
|
|
|
// WarnLevel level. Non-critical entries that deserve eyes.
|
|
|
|
WarnLevel
|
|
|
|
// InfoLevel level. General operational entries about what's going on inside the
|
2014-03-12 18:34:29 +04:00
|
|
|
// application.
|
2014-07-16 02:30:01 +04:00
|
|
|
InfoLevel
|
|
|
|
// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
|
|
|
|
DebugLevel
|
2014-02-23 18:57:04 +04:00
|
|
|
)
|
|
|
|
|
2014-03-14 23:07:57 +04:00
|
|
|
// Won't compile if StdLogger can't be realized by a log.Logger
|
2015-09-08 03:49:55 +03:00
|
|
|
var (
|
|
|
|
_ StdLogger = &log.Logger{}
|
|
|
|
_ StdLogger = &Entry{}
|
|
|
|
_ StdLogger = &Logger{}
|
|
|
|
)
|
2014-03-14 23:07:57 +04:00
|
|
|
|
|
|
|
// StdLogger is what your logrus-enabled library should take, that way
|
2014-02-23 18:57:04 +04:00
|
|
|
// it'll accept a stdlib logger and a logrus logger. There's no standard
|
|
|
|
// interface, this is the closest we get, unfortunately.
|
2014-03-14 23:07:57 +04:00
|
|
|
type StdLogger interface {
|
2014-02-23 18:57:04 +04:00
|
|
|
Print(...interface{})
|
|
|
|
Printf(string, ...interface{})
|
2014-03-14 22:50:08 +04:00
|
|
|
Println(...interface{})
|
2014-02-23 18:57:04 +04:00
|
|
|
|
|
|
|
Fatal(...interface{})
|
|
|
|
Fatalf(string, ...interface{})
|
|
|
|
Fatalln(...interface{})
|
|
|
|
|
|
|
|
Panic(...interface{})
|
|
|
|
Panicf(string, ...interface{})
|
|
|
|
Panicln(...interface{})
|
|
|
|
}
|