Simplify Dur method usage.

This commit is contained in:
Olivier Poitrey 2017-05-20 21:08:42 -07:00
parent e0e86f933f
commit bf4b44614c
6 changed files with 30 additions and 31 deletions

View File

@ -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.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.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. * `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 ## 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. * `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`. * `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`. * `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. * `Dict`: Adds a sub-key/value as a field of the event.
* `Interface`: Uses reflection to marshal the type. * `Interface`: Uses reflection to marshal the type.

View File

@ -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. // 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 { func (c Context) Dur(key string, d time.Duration) Context {
c.l.context = appendDuration(c.l.context, key, d, unit, true) c.l.context = appendDuration(c.l.context, key, d)
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 return c
} }

View File

@ -262,21 +262,14 @@ 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. // Dur adds the fields key with duration d stored as zerolog.DurationFieldUnit.
func (e *Event) Dur(key string, d, unit time.Duration) *Event { // 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 { if !e.enabled {
return e return e
} }
e.buf = appendDuration(e.buf, key, d, unit, true) e.buf = appendDuration(e.buf, key, d)
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 return e
} }

View File

@ -86,11 +86,11 @@ func appendTimestamp(dst []byte) []byte {
return appendTime(dst, TimestampFieldName, TimestampFunc()) return appendTime(dst, TimestampFieldName, TimestampFunc())
} }
func appendDuration(dst []byte, key string, d, unit time.Duration, float bool) []byte { func appendDuration(dst []byte, key string, d time.Duration) []byte {
if float { if DurationFieldInteger {
return appendFloat64(dst, key, float64(d)/float64(unit)) 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 { func appendInterface(dst []byte, key string, i interface{}) []byte {

View File

@ -26,6 +26,14 @@ var (
// TimestampFunc defines the function called to generate a timestamp. // TimestampFunc defines the function called to generate a timestamp.
TimestampFunc = time.Now 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 ( var (

View File

@ -133,16 +133,16 @@ func ExampleEvent_Interface() {
} }
func ExampleEvent_Dur() { func ExampleEvent_Dur() {
d := time.Duration(10 * time.Millisecond) d := time.Duration(10 * time.Second)
log := zerolog.New(os.Stdout) log := zerolog.New(os.Stdout)
log.Log(). log.Log().
Str("foo", "bar"). Str("foo", "bar").
Dur("dur", d, time.Second). Dur("dur", d).
Msg("hello world") Msg("hello world")
// Output: {"foo":"bar","dur":0.01,"message":"hello world"} // Output: {"foo":"bar","dur":10000,"message":"hello world"}
} }
func ExampleContext_Dict() { func ExampleContext_Dict() {
@ -176,14 +176,14 @@ func ExampleContext_Interface() {
} }
func ExampleContext_Dur() { func ExampleContext_Dur() {
d := time.Duration(10 * time.Millisecond) d := time.Duration(10 * time.Second)
log := zerolog.New(os.Stdout).With(). log := zerolog.New(os.Stdout).With().
Str("foo", "bar"). Str("foo", "bar").
Dur("dur", d, time.Second). Dur("dur", d).
Logger() Logger()
log.Log().Msg("hello world") log.Log().Msg("hello world")
// Output: {"foo":"bar","dur":0.01,"message":"hello world"} // Output: {"foo":"bar","dur":10000,"message":"hello world"}
} }