Add support for the duration field

This commit is contained in:
Olivier Poitrey 2017-05-19 22:43:10 -07:00
parent 76d3c39327
commit 195fd3d7c6
4 changed files with 64 additions and 0 deletions

View File

@ -128,6 +128,18 @@ func (c Context) Time(key string, t time.Time) Context {
return c 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. // Interface adds the field key with obj marshaled using reflection.
func (c Context) Interface(key string, i interface{}) Context { func (c Context) Interface(key string, i interface{}) Context {
c.l.context = appendInterface(c.l.context, key, i) c.l.context = appendInterface(c.l.context, key, i)

View File

@ -262,6 +262,24 @@ func (e *Event) Time(key string, t time.Time) *Event {
return e 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. // Interface adds the field key with i marshaled using reflection.
func (e *Event) Interface(key string, i interface{}) *Event { func (e *Event) Interface(key string, i interface{}) *Event {
if !e.enabled { if !e.enabled {

View File

@ -88,6 +88,13 @@ func appendTimestamp(dst []byte) []byte {
return appendTime(dst, TimestampFieldName, now()) 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 { func appendInterface(dst []byte, key string, i interface{}) []byte {
marshaled, err := json.Marshal(i) marshaled, err := json.Marshal(i)
if err != nil { if err != nil {

View File

@ -3,6 +3,7 @@ package zerolog_test
import ( import (
"errors" "errors"
"os" "os"
"time"
"github.com/rs/zerolog" "github.com/rs/zerolog"
) )
@ -131,6 +132,19 @@ func ExampleEvent_Interface() {
// Output: {"foo":"bar","obj":{"name":"john"},"message":"hello world"} // 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() { func ExampleContext_Dict() {
log := zerolog.New(os.Stdout).With(). log := zerolog.New(os.Stdout).With().
Str("foo", "bar"). Str("foo", "bar").
@ -160,3 +174,16 @@ func ExampleContext_Interface() {
// Output: {"foo":"bar","obj":{"name":"john"},"message":"hello world"} // 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"}
}