Always place timestamp as first field for better log readability
This commit is contained in:
parent
2a829377cb
commit
2e3da1d5b5
|
@ -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
11
log.go
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue