1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
package logging
import (
"fmt"
"runtime"
"time"
"github.com/sirupsen/logrus"
)
type Entry struct {
*logrus.Entry
isOnError bool
err error
}
var idKey = "logID"
// SetIDKey key of id in logentry
func SetIDKey(key string) {
idKey = key
}
// Deprecated: Log creates a new entry with an id
func Log(id string) *Entry {
entry := (*logrus.Logger)(log).WithField(idKey, id)
entry.Logger = (*logrus.Logger)(log)
return &Entry{Entry: entry}
}
// Deprecated: LogWithFields creates a new entry with an id and the given fields
func LogWithFields(id string, fields ...interface{}) *Entry {
e := Log(id)
return e.SetFields(fields...)
}
// New instantiates a new entry
func New() *Entry {
return &Entry{Entry: logrus.NewEntry((*logrus.Logger)(log))}
}
func OnError(err error) *Entry {
e := New()
return e.OnError(err)
}
func WithError(err error) *Entry {
e := New()
return e.WithError(err)
}
// WithFields creates a new entry without an id and the given fields
func WithFields(fields ...interface{}) *Entry {
return New().SetFields(fields...)
}
// OnError sets the error. The log will only be printed if err is not nil
func (e *Entry) OnError(err error) *Entry {
e.err = err
e.isOnError = true
return e
}
// SetFields sets the given fields on the entry. It panics if length of fields is odd
func (e *Entry) SetFields(fields ...interface{}) *Entry {
logFields := toFields(fields...)
return e.WithFields(logFields)
}
func (e *Entry) WithField(key string, value interface{}) *Entry {
e.Entry = e.Entry.WithField(key, value)
return e
}
func (e *Entry) WithFields(fields logrus.Fields) *Entry {
e.Entry = e.Entry.WithFields(fields)
return e
}
func (e *Entry) WithError(err error) *Entry {
e.Entry = e.Entry.WithError(err)
return e
}
func (e *Entry) WithTime(t time.Time) *Entry {
e.Entry = e.Entry.WithTime(t)
return e
}
func toFields(fields ...interface{}) logrus.Fields {
if len(fields)%2 != 0 {
return logrus.Fields{"oddFields": len(fields)}
}
logFields := make(logrus.Fields, len(fields)%2)
for i := 0; i < len(fields); i = i + 2 {
key := fields[i].(string)
logFields[key] = fields[i+1]
}
return logFields
}
func Debug(args ...interface{}) {
e := New()
e.log(func() { e.Entry.Debug(args...) })
}
func (e *Entry) Debug(args ...interface{}) {
e.log(func() { e.Entry.Debug(args...) })
}
func Debugln(args ...interface{}) {
e := New()
e.log(func() { e.Entry.Debugln(args...) })
}
func (e *Entry) Debugln(args ...interface{}) {
e.log(func() { e.Entry.Debugln(args...) })
}
func Debugf(format string, args ...interface{}) {
e := New()
e.log(func() { e.Entry.Debugf(format, args...) })
}
func (e *Entry) Debugf(format string, args ...interface{}) {
e.log(func() { e.Entry.Debugf(format, args...) })
}
func Info(args ...interface{}) {
e := New()
e.log(func() { e.Entry.Info(args...) })
}
func (e *Entry) Info(args ...interface{}) {
e.log(func() { e.Entry.Info(args...) })
}
func Infoln(args ...interface{}) {
e := New()
e.log(func() { e.Entry.Infoln(args...) })
}
func (e *Entry) Infoln(args ...interface{}) {
e.log(func() { e.Entry.Infoln(args...) })
}
func Infof(format string, args ...interface{}) {
e := New()
e.log(func() { e.Entry.Infof(format, args...) })
}
func (e *Entry) Infof(format string, args ...interface{}) {
e.log(func() { e.Entry.Infof(format, args...) })
}
func Trace(args ...interface{}) {
e := New()
e.log(func() { e.Entry.Trace(args...) })
}
func (e *Entry) Trace(args ...interface{}) {
e.log(func() { e.Entry.Trace(args...) })
}
func Traceln(args ...interface{}) {
e := New()
e.log(func() { e.Entry.Traceln(args...) })
}
func (e *Entry) Traceln(args ...interface{}) {
e.log(func() { e.Entry.Traceln(args...) })
}
func Tracef(format string, args ...interface{}) {
e := New()
e.log(func() { e.Entry.Tracef(format, args...) })
}
func (e *Entry) Tracef(format string, args ...interface{}) {
e.log(func() { e.Entry.Tracef(format, args...) })
}
func Warn(args ...interface{}) {
e := New()
e.log(func() { e.Entry.Warn(args...) })
}
func (e *Entry) Warn(args ...interface{}) {
e.log(func() { e.Entry.Warn(args...) })
}
func Warnln(args ...interface{}) {
e := New()
e.log(func() { e.Entry.Warnln(args...) })
}
func (e *Entry) Warnln(args ...interface{}) {
e.log(func() { e.Entry.Warnln(args...) })
}
func Warnf(format string, args ...interface{}) {
e := New()
e.log(func() { e.Entry.Warnf(format, args...) })
}
func (e *Entry) Warnf(format string, args ...interface{}) {
e.log(func() { e.Entry.Warnf(format, args...) })
}
func Warning(args ...interface{}) {
e := New()
e.log(func() { e.Entry.Warning(args...) })
}
func (e *Entry) Warning(args ...interface{}) {
e.log(func() { e.Entry.Warning(args...) })
}
func Warningln(args ...interface{}) {
e := New()
e.log(func() { e.Entry.Warningln(args...) })
}
func (e *Entry) Warningln(args ...interface{}) {
e.log(func() { e.Entry.Warningln(args...) })
}
func Warningf(format string, args ...interface{}) {
e := New()
e.log(func() { e.Entry.Warningf(format, args...) })
}
func (e *Entry) Warningf(format string, args ...interface{}) {
e.log(func() { e.Entry.Warningf(format, args...) })
}
func Error(args ...interface{}) {
e := New()
e.log(func() { e.Entry.Error(args...) })
}
func (e *Entry) Error(args ...interface{}) {
e.log(func() { e.Entry.Error(args...) })
}
func Errorln(args ...interface{}) {
e := New()
e.log(func() { e.Entry.Errorln(args...) })
}
func (e *Entry) Errorln(args ...interface{}) {
e.log(func() { e.Entry.Errorln(args...) })
}
func Errorf(format string, args ...interface{}) {
e := New()
e.log(func() { e.Entry.Errorf(format, args...) })
}
func (e *Entry) Errorf(format string, args ...interface{}) {
e.log(func() { e.Entry.Errorf(format, args...) })
}
func Fatal(args ...interface{}) {
e := New()
e.log(func() { e.Entry.Fatal(args...) })
}
func (e *Entry) Fatal(args ...interface{}) {
e.log(func() { e.Entry.Fatal(args...) })
}
func Fatalln(args ...interface{}) {
e := New()
e.log(func() { e.Entry.Fatalln(args...) })
}
func (e *Entry) Fatalln(args ...interface{}) {
e.log(func() { e.Entry.Fatalln(args...) })
}
func Fatalf(format string, args ...interface{}) {
e := New()
e.log(func() { e.Entry.Fatalf(format, args...) })
}
func (e *Entry) Fatalf(format string, args ...interface{}) {
e.log(func() { e.Entry.Fatalf(format, args...) })
}
func Panic(args ...interface{}) {
e := New()
e.log(func() { e.Entry.Panic(args...) })
}
func (e *Entry) Panic(args ...interface{}) {
e.log(func() { e.Entry.Panic(args...) })
}
func Panicln(args ...interface{}) {
e := New()
e.log(func() { e.Entry.Panicln(args...) })
}
func (e *Entry) Panicln(args ...interface{}) {
e.log(func() { e.Entry.Panic(args...) })
}
func Panicf(format string, args ...interface{}) {
e := New()
e.log(func() { e.Entry.Panicf(format, args...) })
}
func (e *Entry) Panicf(format string, args ...interface{}) {
e.log(func() { e.Entry.Panicf(format, args...) })
}
func (e *Entry) Log(level logrus.Level, args ...interface{}) {
e.log(func() { e.Entry.Log(level, args...) })
}
func Logf(level logrus.Level, format string, args ...interface{}) {
e := New()
e.log(func() { e.Entry.Logf(level, format, args...) })
}
func (e *Entry) Logf(level logrus.Level, format string, args ...interface{}) {
e.log(func() { e.Entry.Logf(level, format, args...) })
}
func Logln(level logrus.Level, args ...interface{}) {
e := New()
e.log(func() { e.Entry.Logln(level, args...) })
}
func (e *Entry) Logln(level logrus.Level, args ...interface{}) {
e.log(func() { e.Entry.Logln(level, args...) })
}
func (e *Entry) log(log func()) {
e = e.checkOnError()
if e == nil {
return
}
addCaller(e)
log()
}
func (e *Entry) checkOnError() *Entry {
if !e.isOnError {
return e
}
if e.err != nil {
return e.WithError(e.err)
}
return nil
}
func addCaller(e *Entry) {
_, file, no, ok := runtime.Caller(3)
if ok {
e.WithField("caller", fmt.Sprintf("%s:%d", file, no))
}
}
|