diff --git a/hlog/hlog_example_test.go b/hlog/hlog_example_test.go index ee02ad1..5ffcaf7 100644 --- a/hlog/hlog_example_test.go +++ b/hlog/hlog_example_test.go @@ -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"} } diff --git a/log.go b/log.go index 7b94df2..c0e4e74 100644 --- a/log.go +++ b/log.go @@ -279,22 +279,21 @@ 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 len(e.buf) > 1 { - e.buf = append(e.buf, ',') - } - e.buf = append(e.buf, 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 }