Always place timestamp as first field for better log readability

This commit is contained in:
Olivier Poitrey 2017-06-08 10:03:21 -07:00
parent 2a829377cb
commit 2e3da1d5b5
2 changed files with 9 additions and 10 deletions

View File

@ -67,5 +67,5 @@ func Example_handler() {
h.ServeHTTP(httptest.NewRecorder(), &http.Request{})
// Output: {"level":"info","time":"2001-02-03T04:05:06Z","role":"my-service","host":"local-hostname","user":"current user","status":"ok","message":"Something happend"}
// Output: {"time":"2001-02-03T04:05:06Z","level":"info","role":"my-service","host":"local-hostname","user":"current user","status":"ok","message":"Something happend"}
}

11
log.go
View File

@ -279,23 +279,22 @@ func (l Logger) newEvent(level Level, addLevelField bool, done func(string)) *Ev
}
e := newEvent(l.w, lvl, enabled)
e.done = done
if l.context != nil && len(l.context) > 0 && l.context[0] > 0 {
// first byte of context is ts flag
e.buf = appendTimestamp(e.buf)
}
if addLevelField {
e.Str(LevelFieldName, level.String())
}
if l.sample > 0 && SampleFieldName != "" {
e.Uint32(SampleFieldName, l.sample)
}
if l.context != nil && len(l.context) > 0 {
if l.context[0] > 0 { // ts flag
e.buf = appendTimestamp(e.buf)
}
if len(l.context) > 1 {
if l.context != nil && len(l.context) > 1 {
if len(e.buf) > 1 {
e.buf = append(e.buf, ',')
}
e.buf = append(e.buf, l.context[1:]...)
}
}
return e
}