From 195fd3d7c6c449da97e3114aac5b01ddb85af696 Mon Sep 17 00:00:00 2001 From: Olivier Poitrey Date: Fri, 19 May 2017 22:43:10 -0700 Subject: [PATCH] Add support for the duration field --- context.go | 12 ++++++++++++ event.go | 18 ++++++++++++++++++ field.go | 7 +++++++ log_example_test.go | 27 +++++++++++++++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/context.go b/context.go index 0034066..3b619a2 100644 --- a/context.go +++ b/context.go @@ -128,6 +128,18 @@ func (c Context) Time(key string, t time.Time) Context { return c } +// Dur adds the fields key with d divided by unit and stored as a float. +func (c Context) Dur(key string, d, unit time.Duration) Context { + c.l.context = appendDuration(c.l.context, key, d, unit, true) + return c +} + +// DurInt adds the fields key with d divided by unit and stored as a int. +func (c Context) DurInt(key string, d, unit time.Duration) Context { + c.l.context = appendDuration(c.l.context, key, d, unit, true) + return c +} + // Interface adds the field key with obj marshaled using reflection. func (c Context) Interface(key string, i interface{}) Context { c.l.context = appendInterface(c.l.context, key, i) diff --git a/event.go b/event.go index 7155444..d0b9c7e 100644 --- a/event.go +++ b/event.go @@ -262,6 +262,24 @@ func (e *Event) Time(key string, t time.Time) *Event { return e } +// Dur adds the fields key with d divided by unit and stored as a float. +func (e *Event) Dur(key string, d, unit time.Duration) *Event { + if !e.enabled { + return e + } + e.buf = appendDuration(e.buf, key, d, unit, true) + return e +} + +// DurInt adds the fields key with d divided by unit and stored as a int. +func (e *Event) DurInt(key string, d, unit time.Duration) *Event { + if !e.enabled { + return e + } + e.buf = appendDuration(e.buf, key, d, unit, true) + return e +} + // Interface adds the field key with i marshaled using reflection. func (e *Event) Interface(key string, i interface{}) *Event { if !e.enabled { diff --git a/field.go b/field.go index b00458b..79c23bc 100644 --- a/field.go +++ b/field.go @@ -88,6 +88,13 @@ func appendTimestamp(dst []byte) []byte { return appendTime(dst, TimestampFieldName, now()) } +func appendDuration(dst []byte, key string, d, unit time.Duration, float bool) []byte { + if float { + return appendFloat64(dst, key, float64(d)/float64(unit)) + } + return appendInt64(dst, key, int64(d/unit)) +} + func appendInterface(dst []byte, key string, i interface{}) []byte { marshaled, err := json.Marshal(i) if err != nil { diff --git a/log_example_test.go b/log_example_test.go index 500cd98..a060228 100644 --- a/log_example_test.go +++ b/log_example_test.go @@ -3,6 +3,7 @@ package zerolog_test import ( "errors" "os" + "time" "github.com/rs/zerolog" ) @@ -131,6 +132,19 @@ func ExampleEvent_Interface() { // Output: {"foo":"bar","obj":{"name":"john"},"message":"hello world"} } +func ExampleEvent_Dur() { + d := time.Duration(10 * time.Millisecond) + + log := zerolog.New(os.Stdout) + + log.Log(). + Str("foo", "bar"). + Dur("dur", d, time.Second). + Msg("hello world") + + // Output: {"foo":"bar","dur":0.01,"message":"hello world"} +} + func ExampleContext_Dict() { log := zerolog.New(os.Stdout).With(). Str("foo", "bar"). @@ -160,3 +174,16 @@ func ExampleContext_Interface() { // Output: {"foo":"bar","obj":{"name":"john"},"message":"hello world"} } + +func ExampleContext_Dur() { + d := time.Duration(10 * time.Millisecond) + + log := zerolog.New(os.Stdout).With(). + Str("foo", "bar"). + Dur("dur", d, time.Second). + Logger() + + log.Log().Msg("hello world") + + // Output: {"foo":"bar","dur":0.01,"message":"hello world"} +}