Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame^] | 1 | package logrus |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "log" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | // Fields type, used to pass to `WithFields`. |
| 10 | type Fields map[string]interface{} |
| 11 | |
| 12 | // Level type |
| 13 | type Level uint8 |
| 14 | |
| 15 | // Convert the Level to a string. E.g. PanicLevel becomes "panic". |
| 16 | func (level Level) String() string { |
| 17 | switch level { |
| 18 | case DebugLevel: |
| 19 | return "debug" |
| 20 | case InfoLevel: |
| 21 | return "info" |
| 22 | case WarnLevel: |
| 23 | return "warning" |
| 24 | case ErrorLevel: |
| 25 | return "error" |
| 26 | case FatalLevel: |
| 27 | return "fatal" |
| 28 | case PanicLevel: |
| 29 | return "panic" |
| 30 | } |
| 31 | |
| 32 | return "unknown" |
| 33 | } |
| 34 | |
| 35 | // ParseLevel takes a string level and returns the Logrus log level constant. |
| 36 | func ParseLevel(lvl string) (Level, error) { |
| 37 | switch strings.ToLower(lvl) { |
| 38 | case "panic": |
| 39 | return PanicLevel, nil |
| 40 | case "fatal": |
| 41 | return FatalLevel, nil |
| 42 | case "error": |
| 43 | return ErrorLevel, nil |
| 44 | case "warn", "warning": |
| 45 | return WarnLevel, nil |
| 46 | case "info": |
| 47 | return InfoLevel, nil |
| 48 | case "debug": |
| 49 | return DebugLevel, nil |
| 50 | } |
| 51 | |
| 52 | var l Level |
| 53 | return l, fmt.Errorf("not a valid logrus Level: %q", lvl) |
| 54 | } |
| 55 | |
| 56 | // A constant exposing all logging levels |
| 57 | var AllLevels = []Level{ |
| 58 | PanicLevel, |
| 59 | FatalLevel, |
| 60 | ErrorLevel, |
| 61 | WarnLevel, |
| 62 | InfoLevel, |
| 63 | DebugLevel, |
| 64 | } |
| 65 | |
| 66 | // These are the different logging levels. You can set the logging level to log |
| 67 | // on your instance of logger, obtained with `logrus.New()`. |
| 68 | const ( |
| 69 | // PanicLevel level, highest level of severity. Logs and then calls panic with the |
| 70 | // message passed to Debug, Info, ... |
| 71 | PanicLevel Level = iota |
| 72 | // FatalLevel level. Logs and then calls `os.Exit(1)`. It will exit even if the |
| 73 | // logging level is set to Panic. |
| 74 | FatalLevel |
| 75 | // ErrorLevel level. Logs. Used for errors that should definitely be noted. |
| 76 | // Commonly used for hooks to send errors to an error tracking service. |
| 77 | ErrorLevel |
| 78 | // WarnLevel level. Non-critical entries that deserve eyes. |
| 79 | WarnLevel |
| 80 | // InfoLevel level. General operational entries about what's going on inside the |
| 81 | // application. |
| 82 | InfoLevel |
| 83 | // DebugLevel level. Usually only enabled when debugging. Very verbose logging. |
| 84 | DebugLevel |
| 85 | ) |
| 86 | |
| 87 | // Won't compile if StdLogger can't be realized by a log.Logger |
| 88 | var ( |
| 89 | _ StdLogger = &log.Logger{} |
| 90 | _ StdLogger = &Entry{} |
| 91 | _ StdLogger = &Logger{} |
| 92 | ) |
| 93 | |
| 94 | // StdLogger is what your logrus-enabled library should take, that way |
| 95 | // it'll accept a stdlib logger and a logrus logger. There's no standard |
| 96 | // interface, this is the closest we get, unfortunately. |
| 97 | type StdLogger interface { |
| 98 | Print(...interface{}) |
| 99 | Printf(string, ...interface{}) |
| 100 | Println(...interface{}) |
| 101 | |
| 102 | Fatal(...interface{}) |
| 103 | Fatalf(string, ...interface{}) |
| 104 | Fatalln(...interface{}) |
| 105 | |
| 106 | Panic(...interface{}) |
| 107 | Panicf(string, ...interface{}) |
| 108 | Panicln(...interface{}) |
| 109 | } |
| 110 | |
| 111 | // The FieldLogger interface generalizes the Entry and Logger types |
| 112 | type FieldLogger interface { |
| 113 | WithField(key string, value interface{}) *Entry |
| 114 | WithFields(fields Fields) *Entry |
| 115 | WithError(err error) *Entry |
| 116 | |
| 117 | Debugf(format string, args ...interface{}) |
| 118 | Infof(format string, args ...interface{}) |
| 119 | Printf(format string, args ...interface{}) |
| 120 | Warnf(format string, args ...interface{}) |
| 121 | Warningf(format string, args ...interface{}) |
| 122 | Errorf(format string, args ...interface{}) |
| 123 | Fatalf(format string, args ...interface{}) |
| 124 | Panicf(format string, args ...interface{}) |
| 125 | |
| 126 | Debug(args ...interface{}) |
| 127 | Info(args ...interface{}) |
| 128 | Print(args ...interface{}) |
| 129 | Warn(args ...interface{}) |
| 130 | Warning(args ...interface{}) |
| 131 | Error(args ...interface{}) |
| 132 | Fatal(args ...interface{}) |
| 133 | Panic(args ...interface{}) |
| 134 | |
| 135 | Debugln(args ...interface{}) |
| 136 | Infoln(args ...interface{}) |
| 137 | Println(args ...interface{}) |
| 138 | Warnln(args ...interface{}) |
| 139 | Warningln(args ...interface{}) |
| 140 | Errorln(args ...interface{}) |
| 141 | Fatalln(args ...interface{}) |
| 142 | Panicln(args ...interface{}) |
| 143 | } |