├── LICENSE ├── README.md ├── doc.go └── log.go /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2016 Travis Cline 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # log 4 | `import "github.com/tmc/log"` 5 | 6 | * [Overview](#pkg-overview) 7 | * [Index](#pkg-index) 8 | 9 | ## Overview 10 | Package log wraps logrus to include source line/function information 11 | 12 | inspired by prometheus/common/log 13 | 14 | 15 | 16 | 17 | ## Index 18 | * [func Debug(args ...interface{})](#Debug) 19 | * [func Debugln(args ...interface{})](#Debugln) 20 | * [func Error(args ...interface{})](#Error) 21 | * [func Errorln(args ...interface{})](#Errorln) 22 | * [func Fatal(args ...interface{})](#Fatal) 23 | * [func Fatalln(args ...interface{})](#Fatalln) 24 | * [func Info(args ...interface{})](#Info) 25 | * [func Infoln(args ...interface{})](#Infoln) 26 | * [func SetLevel(level Level)](#SetLevel) 27 | * [func Warn(args ...interface{})](#Warn) 28 | * [func Warnln(args ...interface{})](#Warnln) 29 | * [type Level](#Level) 30 | * [type Logger](#Logger) 31 | * [func Base() Logger](#Base) 32 | * [func New() Logger](#New) 33 | * [func With(key string, value interface{}) Logger](#With) 34 | * [func WithError(err error) Logger](#WithError) 35 | 36 | 37 | #### Package files 38 | [doc.go](/src/github.com/tmc/log/doc.go) [log.go](/src/github.com/tmc/log/log.go) 39 | 40 | 41 | 42 | 43 | 44 | ## func [Debug](/src/target/log.go?s=4370:4401#L164) 45 | ``` go 46 | func Debug(args ...interface{}) 47 | ``` 48 | Debug logs a message at level Debug on the standard logger. 49 | 50 | 51 | 52 | ## func [Debugln](/src/target/log.go?s=4509:4542#L169) 53 | ``` go 54 | func Debugln(args ...interface{}) 55 | ``` 56 | Debugln logs a message at level Debug on the standard logger. 57 | 58 | 59 | 60 | ## func [Error](/src/target/log.go?s=5194:5225#L194) 61 | ``` go 62 | func Error(args ...interface{}) 63 | ``` 64 | Error logs a message at level Error on the standard logger. 65 | 66 | 67 | 68 | ## func [Errorln](/src/target/log.go?s=5333:5366#L199) 69 | ``` go 70 | func Errorln(args ...interface{}) 71 | ``` 72 | Errorln logs a message at level Error on the standard logger. 73 | 74 | 75 | 76 | ## func [Fatal](/src/target/log.go?s=5474:5505#L204) 77 | ``` go 78 | func Fatal(args ...interface{}) 79 | ``` 80 | Fatal logs a message at level Fatal on the standard logger. 81 | 82 | 83 | 84 | ## func [Fatalln](/src/target/log.go?s=5613:5646#L209) 85 | ``` go 86 | func Fatalln(args ...interface{}) 87 | ``` 88 | Fatalln logs a message at level Fatal on the standard logger. 89 | 90 | 91 | 92 | ## func [Info](/src/target/log.go?s=4648:4678#L174) 93 | ``` go 94 | func Info(args ...interface{}) 95 | ``` 96 | Info logs a message at level Info on the standard logger. 97 | 98 | 99 | 100 | ## func [Infoln](/src/target/log.go?s=4783:4815#L179) 101 | ``` go 102 | func Infoln(args ...interface{}) 103 | ``` 104 | Infoln logs a message at level Info on the standard logger. 105 | 106 | 107 | 108 | ## func [SetLevel](/src/target/log.go?s=3912:3938#L149) 109 | ``` go 110 | func SetLevel(level Level) 111 | ``` 112 | SetLevel sets the Level of the base logger 113 | 114 | 115 | 116 | ## func [Warn](/src/target/log.go?s=4920:4950#L184) 117 | ``` go 118 | func Warn(args ...interface{}) 119 | ``` 120 | Warn logs a message at level Warn on the standard logger. 121 | 122 | 123 | 124 | ## func [Warnln](/src/target/log.go?s=5055:5087#L189) 125 | ``` go 126 | func Warnln(args ...interface{}) 127 | ``` 128 | Warnln logs a message at level Warn on the standard logger. 129 | 130 | 131 | 132 | 133 | ## type [Level](/src/target/log.go?s=134:150#L3) 134 | ``` go 135 | type Level uint8 136 | ``` 137 | Level describes the log severity level. 138 | 139 | 140 | ``` go 141 | const ( 142 | // PanicLevel level, highest level of severity. Logs and then calls panic with the 143 | // message passed to Debug, Info, ... 144 | PanicLevel Level = iota 145 | // FatalLevel level. Logs and then calls `os.Exit(1)`. It will exit even if the 146 | // logging level is set to Panic. 147 | FatalLevel 148 | // ErrorLevel level. Logs. Used for errors that should definitely be noted. 149 | // Commonly used for hooks to send errors to an error tracking service. 150 | ErrorLevel 151 | // WarnLevel level. Non-critical entries that deserve eyes. 152 | WarnLevel 153 | // InfoLevel level. General operational entries about what's going on inside the 154 | // application. 155 | InfoLevel 156 | // DebugLevel level. Usually only enabled when debugging. Very verbose logging. 157 | DebugLevel 158 | ) 159 | ``` 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | ## type [Logger](/src/target/log.go?s=926:1312#L25) 170 | ``` go 171 | type Logger interface { 172 | SetLevel(level Level) 173 | SetOut(out io.Writer) 174 | 175 | Debug(...interface{}) 176 | Debugln(...interface{}) 177 | 178 | Info(...interface{}) 179 | Infoln(...interface{}) 180 | 181 | Warn(...interface{}) 182 | Warnln(...interface{}) 183 | 184 | Error(...interface{}) 185 | Errorln(...interface{}) 186 | 187 | Fatal(...interface{}) 188 | Fatalln(...interface{}) 189 | 190 | With(key string, value interface{}) Logger 191 | WithError(err error) Logger 192 | } 193 | ``` 194 | Logger is an interface that describes logging. 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | ### func [Base](/src/target/log.go?s=3823:3841#L144) 203 | ``` go 204 | func Base() Logger 205 | ``` 206 | Base returns the base logger. 207 | 208 | 209 | ### func [New](/src/target/log.go?s=3716:3733#L139) 210 | ``` go 211 | func New() Logger 212 | ``` 213 | New returns a new logger. 214 | 215 | 216 | ### func [With](/src/target/log.go?s=4037:4084#L154) 217 | ``` go 218 | func With(key string, value interface{}) Logger 219 | ``` 220 | With attaches a key,value pair to a logger. 221 | 222 | 223 | ### func [WithError](/src/target/log.go?s=4210:4242#L159) 224 | ``` go 225 | func WithError(err error) Logger 226 | ``` 227 | WithError returns a Logger that will print an error along with the next message. 228 | 229 | 230 | 231 | -------------------------------------------------------------------------------- /doc.go: -------------------------------------------------------------------------------- 1 | // Package log wraps logrus to include source line/function information 2 | // 3 | // 4 | // 5 | // inspired by prometheus/common/log 6 | package log 7 | -------------------------------------------------------------------------------- /log.go: -------------------------------------------------------------------------------- 1 | package log 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "runtime" 7 | "strings" 8 | 9 | "github.com/sirupsen/logrus" 10 | ) 11 | 12 | // Level describes the log severity level. 13 | type Level uint8 14 | 15 | const ( 16 | // PanicLevel level, highest level of severity. Logs and then calls panic with the 17 | // message passed to Debug, Info, ... 18 | PanicLevel Level = iota 19 | // FatalLevel level. Logs and then calls `os.Exit(1)`. It will exit even if the 20 | // logging level is set to Panic. 21 | FatalLevel 22 | // ErrorLevel level. Logs. Used for errors that should definitely be noted. 23 | // Commonly used for hooks to send errors to an error tracking service. 24 | ErrorLevel 25 | // WarnLevel level. Non-critical entries that deserve eyes. 26 | WarnLevel 27 | // InfoLevel level. General operational entries about what's going on inside the 28 | // application. 29 | InfoLevel 30 | // DebugLevel level. Usually only enabled when debugging. Very verbose logging. 31 | DebugLevel 32 | ) 33 | 34 | // Logger is an interface that describes logging. 35 | type Logger interface { 36 | SetLevel(level Level) 37 | SetOut(out io.Writer) 38 | 39 | Debug(...interface{}) 40 | Debugln(...interface{}) 41 | 42 | Info(...interface{}) 43 | Infoln(...interface{}) 44 | 45 | Warn(...interface{}) 46 | Warnln(...interface{}) 47 | 48 | Error(...interface{}) 49 | Errorln(...interface{}) 50 | 51 | Fatal(...interface{}) 52 | Fatalln(...interface{}) 53 | 54 | With(key string, value interface{}) Logger 55 | WithError(err error) Logger 56 | } 57 | 58 | type logger struct { 59 | entry *logrus.Entry 60 | } 61 | 62 | // With attaches a key-value pair to a logger. 63 | func (l logger) With(key string, value interface{}) Logger { 64 | return logger{l.entry.WithField(key, value)} 65 | } 66 | 67 | // WithError attaches an error to a logger. 68 | func (l logger) WithError(err error) Logger { 69 | return logger{l.entry.WithError(err)} 70 | } 71 | 72 | // SetLevel sets the level of a logger. 73 | func (l logger) SetLevel(level Level) { 74 | l.entry.Logger.Level = logrus.Level(level) 75 | } 76 | 77 | // SetOut sets the output destination for a logger. 78 | func (l logger) SetOut(out io.Writer) { 79 | l.entry.Logger.Out = out 80 | } 81 | 82 | // Debug logs a message at level Debug on the standard logger. 83 | func (l logger) Debug(args ...interface{}) { 84 | l.sourced().Debug(args...) 85 | } 86 | 87 | // Debugln logs a message at level Debug on the standard logger. 88 | func (l logger) Debugln(args ...interface{}) { 89 | l.sourced().Debugln(args...) 90 | } 91 | 92 | // Info logs a message at level Info on the standard logger. 93 | func (l logger) Info(args ...interface{}) { 94 | l.sourced().Info(args...) 95 | } 96 | 97 | // Infoln logs a message at level Info on the standard logger. 98 | func (l logger) Infoln(args ...interface{}) { 99 | l.sourced().Infoln(args...) 100 | } 101 | 102 | // Warn logs a message at level Warn on the standard logger. 103 | func (l logger) Warn(args ...interface{}) { 104 | l.sourced().Warn(args...) 105 | } 106 | 107 | // Warnln logs a message at level Warn on the standard logger. 108 | func (l logger) Warnln(args ...interface{}) { 109 | l.sourced().Warnln(args...) 110 | } 111 | 112 | // Error logs a message at level Error on the standard logger. 113 | func (l logger) Error(args ...interface{}) { 114 | l.sourced().Error(args...) 115 | } 116 | 117 | // Errorln logs a message at level Error on the standard logger. 118 | func (l logger) Errorln(args ...interface{}) { 119 | l.sourced().Errorln(args...) 120 | } 121 | 122 | // Fatal logs a message at level Fatal on the standard logger. 123 | func (l logger) Fatal(args ...interface{}) { 124 | l.sourced().Fatal(args...) 125 | } 126 | 127 | // Fatalln logs a message at level Fatal on the standard logger. 128 | func (l logger) Fatalln(args ...interface{}) { 129 | l.sourced().Fatalln(args...) 130 | } 131 | 132 | // sourced adds a source field to the logger that contains 133 | // the file name and line where the logging happened. 134 | func (l logger) sourced() *logrus.Entry { 135 | pc, file, line, ok := runtime.Caller(2) 136 | fn := "(unknown)" 137 | if !ok { 138 | file = "" 139 | line = 1 140 | } else { 141 | slash := strings.LastIndex(file, "/") 142 | file = file[slash+1:] 143 | fn = runtime.FuncForPC(pc).Name() 144 | } 145 | logger := l.entry.WithField("source", fmt.Sprintf("%s:%d", file, line)) 146 | return logger.WithField("source_func", fn) 147 | } 148 | 149 | var origLogger = logrus.New() 150 | var baseLogger = logger{entry: logrus.NewEntry(origLogger)} 151 | 152 | // New returns a new logger. 153 | func New() Logger { 154 | return logger{entry: logrus.NewEntry(origLogger)} 155 | } 156 | 157 | // Base returns the base logger. 158 | func Base() Logger { 159 | return baseLogger 160 | } 161 | 162 | // SetLevel sets the Level of the base logger 163 | func SetLevel(level Level) { 164 | baseLogger.entry.Logger.Level = logrus.Level(level) 165 | } 166 | 167 | // SetOut sets the output destination base logger 168 | func SetOut(out io.Writer) { 169 | baseLogger.entry.Logger.Out = out 170 | } 171 | 172 | // With attaches a key,value pair to a logger. 173 | func With(key string, value interface{}) Logger { 174 | return baseLogger.With(key, value) 175 | } 176 | 177 | // WithError returns a Logger that will print an error along with the next message. 178 | func WithError(err error) Logger { 179 | return logger{entry: baseLogger.sourced().WithError(err)} 180 | } 181 | 182 | // Debug logs a message at level Debug on the standard logger. 183 | func Debug(args ...interface{}) { 184 | baseLogger.sourced().Debug(args...) 185 | } 186 | 187 | // Debugln logs a message at level Debug on the standard logger. 188 | func Debugln(args ...interface{}) { 189 | baseLogger.sourced().Debugln(args...) 190 | } 191 | 192 | // Info logs a message at level Info on the standard logger. 193 | func Info(args ...interface{}) { 194 | baseLogger.sourced().Info(args...) 195 | } 196 | 197 | // Infoln logs a message at level Info on the standard logger. 198 | func Infoln(args ...interface{}) { 199 | baseLogger.sourced().Infoln(args...) 200 | } 201 | 202 | // Warn logs a message at level Warn on the standard logger. 203 | func Warn(args ...interface{}) { 204 | baseLogger.sourced().Warn(args...) 205 | } 206 | 207 | // Warnln logs a message at level Warn on the standard logger. 208 | func Warnln(args ...interface{}) { 209 | baseLogger.sourced().Warnln(args...) 210 | } 211 | 212 | // Error logs a message at level Error on the standard logger. 213 | func Error(args ...interface{}) { 214 | baseLogger.sourced().Error(args...) 215 | } 216 | 217 | // Errorln logs a message at level Error on the standard logger. 218 | func Errorln(args ...interface{}) { 219 | baseLogger.sourced().Errorln(args...) 220 | } 221 | 222 | // Fatal logs a message at level Fatal on the standard logger. 223 | func Fatal(args ...interface{}) { 224 | baseLogger.sourced().Fatal(args...) 225 | } 226 | 227 | // Fatalln logs a message at level Fatal on the standard logger. 228 | func Fatalln(args ...interface{}) { 229 | baseLogger.sourced().Fatalln(args...) 230 | } 231 | --------------------------------------------------------------------------------