diff --git a/README.md b/README.md index 8300246..a59fb5e 100644 --- a/README.md +++ b/README.md @@ -211,6 +211,10 @@ Some settings can be changed and will by applied to all loggers: * `zerolog.ErrorFieldName`: Can be set to customize `Err` field name. * `zerolog.SampleFieldName`: Can be set to customize the field name added when sampling is enabled. * `zerolog.TimeFieldFormat`: Can be set to customize `Time` field value formatting. If set with an empty string, times are formated as UNIX timestamp. + // DurationFieldUnit defines the unit for time.Duration type fields added + // using the Dur method. +* `DurationFieldUnit`: Sets the unit of the fields added by `Dur` (default: `time.Millisecond`). +* `DurationFieldInteger`: If set to true, `Dur` fields are formatted as integers instead of floats. ## Field Types @@ -227,7 +231,7 @@ Some settings can be changed and will by applied to all loggers: * `Err`: Takes an `error` and render it as a string using the `zerolog.ErrorFieldName` field name. * `Timestamp`: Insert a timestamp field with `zerolog.TimestampFieldName` field name and formatted using `zerolog.TimeFieldFormat`. * `Time`: Adds a field with the time formated with the `zerolog.TimeFieldFormat`. -* `Dur`: Adds a field with a `time.Duration` formatted as a `float`. For `int` value, use `DurInt`. +* `Dur`: Adds a field with a `time.Duration`. * `Dict`: Adds a sub-key/value as a field of the event. * `Interface`: Uses reflection to marshal the type. diff --git a/context.go b/context.go index 3b619a2..6a79c00 100644 --- a/context.go +++ b/context.go @@ -129,14 +129,8 @@ func (c Context) Time(key string, t time.Time) Context { } // 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) +func (c Context) Dur(key string, d time.Duration) Context { + c.l.context = appendDuration(c.l.context, key, d) return c } diff --git a/event.go b/event.go index 91bff3a..d15dda7 100644 --- a/event.go +++ b/event.go @@ -262,21 +262,14 @@ 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 { +// Dur adds the fields key with duration d stored as zerolog.DurationFieldUnit. +// If zerolog.DurationFieldInteger is true, durations are rendered as integer +// instead of float. +func (e *Event) Dur(key string, d 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) + e.buf = appendDuration(e.buf, key, d) return e } diff --git a/field.go b/field.go index 03c074f..0867119 100644 --- a/field.go +++ b/field.go @@ -86,11 +86,11 @@ func appendTimestamp(dst []byte) []byte { return appendTime(dst, TimestampFieldName, TimestampFunc()) } -func appendDuration(dst []byte, key string, d, unit time.Duration, float bool) []byte { - if float { - return appendFloat64(dst, key, float64(d)/float64(unit)) +func appendDuration(dst []byte, key string, d time.Duration) []byte { + if DurationFieldInteger { + return appendInt64(dst, key, int64(d/DurationFieldUnit)) } - return appendInt64(dst, key, int64(d/unit)) + return appendFloat64(dst, key, float64(d)/float64(DurationFieldUnit)) } func appendInterface(dst []byte, key string, i interface{}) []byte { diff --git a/globals.go b/globals.go index 3620467..e7cff54 100644 --- a/globals.go +++ b/globals.go @@ -26,6 +26,14 @@ var ( // TimestampFunc defines the function called to generate a timestamp. TimestampFunc = time.Now + + // DurationFieldUnit defines the unit for time.Duration type fields added + // using the Dur method. + DurationFieldUnit = time.Millisecond + + // DurationFieldInteger renders Dur fields as integer instead of float if + // set to true. + DurationFieldInteger = false ) var ( diff --git a/log_example_test.go b/log_example_test.go index a060228..863f3c1 100644 --- a/log_example_test.go +++ b/log_example_test.go @@ -133,16 +133,16 @@ func ExampleEvent_Interface() { } func ExampleEvent_Dur() { - d := time.Duration(10 * time.Millisecond) + d := time.Duration(10 * time.Second) log := zerolog.New(os.Stdout) log.Log(). Str("foo", "bar"). - Dur("dur", d, time.Second). + Dur("dur", d). Msg("hello world") - // Output: {"foo":"bar","dur":0.01,"message":"hello world"} + // Output: {"foo":"bar","dur":10000,"message":"hello world"} } func ExampleContext_Dict() { @@ -176,14 +176,14 @@ func ExampleContext_Interface() { } func ExampleContext_Dur() { - d := time.Duration(10 * time.Millisecond) + d := time.Duration(10 * time.Second) log := zerolog.New(os.Stdout).With(). Str("foo", "bar"). - Dur("dur", d, time.Second). + Dur("dur", d). Logger() log.Log().Msg("hello world") - // Output: {"foo":"bar","dur":0.01,"message":"hello world"} + // Output: {"foo":"bar","dur":10000,"message":"hello world"} }