Add support for the duration field
This commit is contained in:
parent
76d3c39327
commit
195fd3d7c6
12
context.go
12
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)
|
||||
|
|
18
event.go
18
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 {
|
||||
|
|
7
field.go
7
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 {
|
||||
|
|
|
@ -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"}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue