Skip to content

Commit

Permalink
Add support for the duration field
Browse files Browse the repository at this point in the history
  • Loading branch information
rs committed May 20, 2017
1 parent 76d3c39 commit 195fd3d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
12 changes: 12 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions log_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package zerolog_test
import (
"errors"
"os"
"time"

"github.com/rs/zerolog"
)
Expand Down Expand Up @@ -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").
Expand Down Expand Up @@ -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"}
}

0 comments on commit 195fd3d

Please sign in to comment.