Skip to content

Commit

Permalink
Use new hook internally to handle timestamp in context
Browse files Browse the repository at this point in the history
  • Loading branch information
rs committed Feb 7, 2018
1 parent cbec237 commit fcbdf23
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
14 changes: 9 additions & 5 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,18 @@ func (c Context) Floats64(key string, f []float64) Context {
return c
}

type timestampHook struct{}

func (ts timestampHook) Run(e *Event, level Level, msg string) {
e.Timestamp()
}

var th = timestampHook{}

// Timestamp adds the current local time as UNIX timestamp to the logger context with the "time" key.
// To customize the key name, change zerolog.TimestampFieldName.
func (c Context) Timestamp() Context {
if len(c.l.context) > 0 {
c.l.context[0] = 1
} else {
c.l.context = append(c.l.context, 1)
}
c.l = c.l.Hook(th)
return c
}

Expand Down
9 changes: 9 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Event struct {
w LevelWriter
level Level
done func(msg string)
ch []Hook // hooks from context
h []Hook
}

Expand Down Expand Up @@ -77,6 +78,14 @@ func (e *Event) Msg(msg string) {
if e == nil {
return
}
if len(e.ch) > 0 {
e.ch[0].Run(e, e.level, msg)
if len(e.ch) > 1 {
for _, hook := range e.ch[1:] {
hook.Run(e, e.level, msg)
}
}
}
if len(e.h) > 0 {
e.h[0].Run(e, e.level, msg)
if len(e.h) > 1 {
Expand Down
2 changes: 1 addition & 1 deletion hlog/hlog_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ func Example_handler() {

h.ServeHTTP(httptest.NewRecorder(), &http.Request{})

// Output: {"time":"2001-02-03T04:05:06Z","level":"info","role":"my-service","host":"local-hostname","user":"current user","status":"ok","message":"Something happend"}
// Output: {"level":"info","role":"my-service","host":"local-hostname","user":"current user","status":"ok","time":"2001-02-03T04:05:06Z","message":"Something happend"}
}
19 changes: 4 additions & 15 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ import (
"io/ioutil"
"os"
"strconv"

"github.com/rs/zerolog/internal/json"
)

// Level defines log levels.
Expand Down Expand Up @@ -193,9 +191,6 @@ func (l Logger) With() Context {
l.context = make([]byte, 0, 500)
if context != nil {
l.context = append(l.context, context...)
} else {
// first byte of context is presence of timestamp or not
l.context = append(l.context, 0)
}
return Context{l}
}
Expand All @@ -208,7 +203,7 @@ func (l *Logger) UpdateContext(update func(c Context) Context) {
return
}
if cap(l.context) == 0 {
l.context = make([]byte, 1, 500) // first byte is timestamp flag
l.context = make([]byte, 0, 500)
}
c := update(Context{*l})
l.context = c.l.context
Expand Down Expand Up @@ -345,21 +340,15 @@ func (l *Logger) newEvent(level Level, done func(string)) *Event {
}
e := newEvent(l.w, level, true)
e.done = done
if l.context != nil && len(l.context) > 0 && l.context[0] > 0 {
// first byte of context is ts flag
e.buf = json.AppendTime(json.AppendKey(e.buf, TimestampFieldName), TimestampFunc(), TimeFieldFormat)
}
e.ch = l.hooks
if level != NoLevel {
e.Str(LevelFieldName, level.String())
}
if l.context != nil && len(l.context) > 1 {
if len(l.context) > 0 {
if len(e.buf) > 1 {
e.buf = append(e.buf, ',')
}
e.buf = append(e.buf, l.context[1:]...)
}
if len(l.hooks) > 0 {
e.h = append(e.h, l.hooks...)
e.buf = append(e.buf, l.context...)
}
return e
}
Expand Down
4 changes: 2 additions & 2 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func TestContextTimestamp(t *testing.T) {
log := New(out).With().Timestamp().Str("foo", "bar").Logger()
log.Log().Msg("hello world")

if got, want := out.String(), `{"time":"2001-02-03T04:05:06Z","foo":"bar","message":"hello world"}`+"\n"; got != want {
if got, want := out.String(), `{"foo":"bar","time":"2001-02-03T04:05:06Z","message":"hello world"}`+"\n"; got != want {
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
}
}
Expand Down Expand Up @@ -470,7 +470,7 @@ func TestOutputWithTimestamp(t *testing.T) {
log := New(ignoredOut).Output(out).With().Timestamp().Str("foo", "bar").Logger()
log.Log().Msg("hello world")

if got, want := out.String(), `{"time":"2001-02-03T04:05:06Z","foo":"bar","message":"hello world"}`+"\n"; got != want {
if got, want := out.String(), `{"foo":"bar","time":"2001-02-03T04:05:06Z","message":"hello world"}`+"\n"; got != want {
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
}
}

0 comments on commit fcbdf23

Please sign in to comment.