David K. Bainbridge | ee02c8e | 2016-11-09 09:13:46 -0800 | [diff] [blame] | 1 | package logrus |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "os" |
| 6 | "sync" |
| 7 | ) |
| 8 | |
| 9 | type Logger struct { |
| 10 | // The logs are `io.Copy`'d to this in a mutex. It's common to set this to a |
| 11 | // file, or leave it default which is `os.Stderr`. You can also set this to |
| 12 | // something more adventorous, such as logging to Kafka. |
| 13 | Out io.Writer |
| 14 | // Hooks for the logger instance. These allow firing events based on logging |
| 15 | // levels and log entries. For example, to send errors to an error tracking |
| 16 | // service, log to StatsD or dump the core on fatal errors. |
| 17 | Hooks LevelHooks |
| 18 | // All log entries pass through the formatter before logged to Out. The |
| 19 | // included formatters are `TextFormatter` and `JSONFormatter` for which |
| 20 | // TextFormatter is the default. In development (when a TTY is attached) it |
| 21 | // logs with colors, but to a file it wouldn't. You can easily implement your |
| 22 | // own that implements the `Formatter` interface, see the `README` or included |
| 23 | // formatters for examples. |
| 24 | Formatter Formatter |
| 25 | // The logging level the logger should log at. This is typically (and defaults |
| 26 | // to) `logrus.Info`, which allows Info(), Warn(), Error() and Fatal() to be |
| 27 | // logged. `logrus.Debug` is useful in |
| 28 | Level Level |
| 29 | // Used to sync writing to the log. |
| 30 | mu sync.Mutex |
| 31 | } |
| 32 | |
| 33 | // Creates a new logger. Configuration should be set by changing `Formatter`, |
| 34 | // `Out` and `Hooks` directly on the default logger instance. You can also just |
| 35 | // instantiate your own: |
| 36 | // |
| 37 | // var log = &Logger{ |
| 38 | // Out: os.Stderr, |
| 39 | // Formatter: new(JSONFormatter), |
| 40 | // Hooks: make(LevelHooks), |
| 41 | // Level: logrus.DebugLevel, |
| 42 | // } |
| 43 | // |
| 44 | // It's recommended to make this a global instance called `log`. |
| 45 | func New() *Logger { |
| 46 | return &Logger{ |
| 47 | Out: os.Stderr, |
| 48 | Formatter: new(TextFormatter), |
| 49 | Hooks: make(LevelHooks), |
| 50 | Level: InfoLevel, |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Adds a field to the log entry, note that you it doesn't log until you call |
| 55 | // Debug, Print, Info, Warn, Fatal or Panic. It only creates a log entry. |
| 56 | // If you want multiple fields, use `WithFields`. |
| 57 | func (logger *Logger) WithField(key string, value interface{}) *Entry { |
| 58 | return NewEntry(logger).WithField(key, value) |
| 59 | } |
| 60 | |
| 61 | // Adds a struct of fields to the log entry. All it does is call `WithField` for |
| 62 | // each `Field`. |
| 63 | func (logger *Logger) WithFields(fields Fields) *Entry { |
| 64 | return NewEntry(logger).WithFields(fields) |
| 65 | } |
| 66 | |
| 67 | // Add an error as single field to the log entry. All it does is call |
| 68 | // `WithError` for the given `error`. |
| 69 | func (logger *Logger) WithError(err error) *Entry { |
| 70 | return NewEntry(logger).WithError(err) |
| 71 | } |
| 72 | |
| 73 | func (logger *Logger) Debugf(format string, args ...interface{}) { |
| 74 | if logger.Level >= DebugLevel { |
| 75 | NewEntry(logger).Debugf(format, args...) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func (logger *Logger) Infof(format string, args ...interface{}) { |
| 80 | if logger.Level >= InfoLevel { |
| 81 | NewEntry(logger).Infof(format, args...) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func (logger *Logger) Printf(format string, args ...interface{}) { |
| 86 | NewEntry(logger).Printf(format, args...) |
| 87 | } |
| 88 | |
| 89 | func (logger *Logger) Warnf(format string, args ...interface{}) { |
| 90 | if logger.Level >= WarnLevel { |
| 91 | NewEntry(logger).Warnf(format, args...) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func (logger *Logger) Warningf(format string, args ...interface{}) { |
| 96 | if logger.Level >= WarnLevel { |
| 97 | NewEntry(logger).Warnf(format, args...) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func (logger *Logger) Errorf(format string, args ...interface{}) { |
| 102 | if logger.Level >= ErrorLevel { |
| 103 | NewEntry(logger).Errorf(format, args...) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func (logger *Logger) Fatalf(format string, args ...interface{}) { |
| 108 | if logger.Level >= FatalLevel { |
| 109 | NewEntry(logger).Fatalf(format, args...) |
| 110 | } |
| 111 | os.Exit(1) |
| 112 | } |
| 113 | |
| 114 | func (logger *Logger) Panicf(format string, args ...interface{}) { |
| 115 | if logger.Level >= PanicLevel { |
| 116 | NewEntry(logger).Panicf(format, args...) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func (logger *Logger) Debug(args ...interface{}) { |
| 121 | if logger.Level >= DebugLevel { |
| 122 | NewEntry(logger).Debug(args...) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | func (logger *Logger) Info(args ...interface{}) { |
| 127 | if logger.Level >= InfoLevel { |
| 128 | NewEntry(logger).Info(args...) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func (logger *Logger) Print(args ...interface{}) { |
| 133 | NewEntry(logger).Info(args...) |
| 134 | } |
| 135 | |
| 136 | func (logger *Logger) Warn(args ...interface{}) { |
| 137 | if logger.Level >= WarnLevel { |
| 138 | NewEntry(logger).Warn(args...) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | func (logger *Logger) Warning(args ...interface{}) { |
| 143 | if logger.Level >= WarnLevel { |
| 144 | NewEntry(logger).Warn(args...) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | func (logger *Logger) Error(args ...interface{}) { |
| 149 | if logger.Level >= ErrorLevel { |
| 150 | NewEntry(logger).Error(args...) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | func (logger *Logger) Fatal(args ...interface{}) { |
| 155 | if logger.Level >= FatalLevel { |
| 156 | NewEntry(logger).Fatal(args...) |
| 157 | } |
| 158 | os.Exit(1) |
| 159 | } |
| 160 | |
| 161 | func (logger *Logger) Panic(args ...interface{}) { |
| 162 | if logger.Level >= PanicLevel { |
| 163 | NewEntry(logger).Panic(args...) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | func (logger *Logger) Debugln(args ...interface{}) { |
| 168 | if logger.Level >= DebugLevel { |
| 169 | NewEntry(logger).Debugln(args...) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | func (logger *Logger) Infoln(args ...interface{}) { |
| 174 | if logger.Level >= InfoLevel { |
| 175 | NewEntry(logger).Infoln(args...) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | func (logger *Logger) Println(args ...interface{}) { |
| 180 | NewEntry(logger).Println(args...) |
| 181 | } |
| 182 | |
| 183 | func (logger *Logger) Warnln(args ...interface{}) { |
| 184 | if logger.Level >= WarnLevel { |
| 185 | NewEntry(logger).Warnln(args...) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | func (logger *Logger) Warningln(args ...interface{}) { |
| 190 | if logger.Level >= WarnLevel { |
| 191 | NewEntry(logger).Warnln(args...) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | func (logger *Logger) Errorln(args ...interface{}) { |
| 196 | if logger.Level >= ErrorLevel { |
| 197 | NewEntry(logger).Errorln(args...) |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | func (logger *Logger) Fatalln(args ...interface{}) { |
| 202 | if logger.Level >= FatalLevel { |
| 203 | NewEntry(logger).Fatalln(args...) |
| 204 | } |
| 205 | os.Exit(1) |
| 206 | } |
| 207 | |
| 208 | func (logger *Logger) Panicln(args ...interface{}) { |
| 209 | if logger.Level >= PanicLevel { |
| 210 | NewEntry(logger).Panicln(args...) |
| 211 | } |
| 212 | } |