1
0
mirror of https://github.com/rs/zerolog synced 2026-06-08 17:13:30 +00:00

feat: add new global DurationFieldFormat (#733)

This deprecates DurationFieldInteger in favor of DurationFieldFormat with the value of DurationFormatInt
This commit is contained in:
Sebastian Rühl
2026-01-05 19:16:40 +01:00
committed by GitHub
parent 05348ba6f6
commit 4685edb80b
13 changed files with 1010 additions and 688 deletions
+1 -1
View File
@@ -193,7 +193,7 @@ func (a *Array) Time(t time.Time) *Array {
// Dur appends d to the array.
func (a *Array) Dur(d time.Duration) *Array {
a.buf = enc.AppendDuration(enc.AppendArrayDelim(a.buf), d, DurationFieldUnit, DurationFieldInteger, FloatingPointPrecision)
a.buf = enc.AppendDuration(enc.AppendArrayDelim(a.buf), d, DurationFieldUnit, DurationFieldFormat, DurationFieldInteger, FloatingPointPrecision)
return a
}
+2 -2
View File
@@ -400,13 +400,13 @@ func (c Context) Times(key string, t []time.Time) Context {
// Dur adds the field key with d divided by unit and stored as a float.
func (c Context) Dur(key string, d time.Duration) Context {
c.l.context = enc.AppendDuration(enc.AppendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger, FloatingPointPrecision)
c.l.context = enc.AppendDuration(enc.AppendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldFormat, DurationFieldInteger, FloatingPointPrecision)
return c
}
// Durs adds the field key with d divided by unit and stored as a float.
func (c Context) Durs(key string, d []time.Duration) Context {
c.l.context = enc.AppendDurations(enc.AppendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger, FloatingPointPrecision)
c.l.context = enc.AppendDurations(enc.AppendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldFormat, DurationFieldInteger, FloatingPointPrecision)
return c
}
+2 -2
View File
@@ -13,8 +13,8 @@ type encoder interface {
AppendBool(dst []byte, val bool) []byte
AppendBools(dst []byte, vals []bool) []byte
AppendBytes(dst, s []byte) []byte
AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool, precision int) []byte
AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool, precision int) []byte
AppendDuration(dst []byte, d time.Duration, unit time.Duration, format string, useInt bool, precision int) []byte
AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, format string, useInt bool, precision int) []byte
AppendEndMarker(dst []byte) []byte
AppendFloat32(dst []byte, val float32, precision int) []byte
AppendFloat64(dst []byte, val float64, precision int) []byte
+3 -3
View File
@@ -729,7 +729,7 @@ func (e *Event) Dur(key string, d time.Duration) *Event {
if e == nil {
return e
}
e.buf = enc.AppendDuration(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger, FloatingPointPrecision)
e.buf = enc.AppendDuration(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldFormat, DurationFieldInteger, FloatingPointPrecision)
return e
}
@@ -740,7 +740,7 @@ func (e *Event) Durs(key string, d []time.Duration) *Event {
if e == nil {
return e
}
e.buf = enc.AppendDurations(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger, FloatingPointPrecision)
e.buf = enc.AppendDurations(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldFormat, DurationFieldInteger, FloatingPointPrecision)
return e
}
@@ -755,7 +755,7 @@ func (e *Event) TimeDiff(key string, t time.Time, start time.Time) *Event {
if t.After(start) {
d = t.Sub(start)
}
e.buf = enc.AppendDuration(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger, FloatingPointPrecision)
e.buf = enc.AppendDuration(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldFormat, DurationFieldInteger, FloatingPointPrecision)
return e
}
+3 -3
View File
@@ -151,7 +151,7 @@ func appendFieldList(dst []byte, kvList []interface{}, stack bool) []byte {
case time.Time:
dst = enc.AppendTime(dst, val, TimeFieldFormat)
case time.Duration:
dst = enc.AppendDuration(dst, val, DurationFieldUnit, DurationFieldInteger, FloatingPointPrecision)
dst = enc.AppendDuration(dst, val, DurationFieldUnit, DurationFieldFormat, DurationFieldInteger, FloatingPointPrecision)
case *string:
if val != nil {
dst = enc.AppendString(dst, *val)
@@ -244,7 +244,7 @@ func appendFieldList(dst []byte, kvList []interface{}, stack bool) []byte {
}
case *time.Duration:
if val != nil {
dst = enc.AppendDuration(dst, *val, DurationFieldUnit, DurationFieldInteger, FloatingPointPrecision)
dst = enc.AppendDuration(dst, *val, DurationFieldUnit, DurationFieldFormat, DurationFieldInteger, FloatingPointPrecision)
} else {
dst = enc.AppendNil(dst)
}
@@ -278,7 +278,7 @@ func appendFieldList(dst []byte, kvList []interface{}, stack bool) []byte {
case []time.Time:
dst = enc.AppendTimes(dst, val, TimeFieldFormat)
case []time.Duration:
dst = enc.AppendDurations(dst, val, DurationFieldUnit, DurationFieldInteger, FloatingPointPrecision)
dst = enc.AppendDurations(dst, val, DurationFieldUnit, DurationFieldFormat, DurationFieldInteger, FloatingPointPrecision)
case nil:
dst = enc.AppendNil(dst)
case net.IP:
+14
View File
@@ -24,6 +24,16 @@ const (
// TimeFormatUnixNano defines a time format that makes time fields to be
// serialized as Unix timestamp integers in nanoseconds.
TimeFormatUnixNano = "UNIXNANO"
// DurationFormatFloat defines a format for Duration fields that makes duration fields to be
// serialized as floating point numbers.
DurationFormatFloat = "float"
// DurationFormatInt defines a format for Duration fields that makes duration fields to be
// serialized as integers.
DurationFormatInt = "int"
// DurationFormatString defines a format for Duration fields that makes duration fields to be
// serialized as string.
DurationFormatString = "string"
)
var (
@@ -107,12 +117,16 @@ var (
// TimestampFunc defines the function called to generate a timestamp.
TimestampFunc = time.Now
// DurationFieldFormat defines the format of the Duration field type.
DurationFieldFormat = DurationFormatFloat
// 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.
// Deprecated: use DurationFieldFormat with DurationFormatInt instead.
DurationFieldInteger = false
// ErrorHandler is called whenever zerolog fails to write an event on its
+22 -3
View File
@@ -4,6 +4,17 @@ import (
"time"
)
const (
// Import from zerolog/global.go
timeFormatUnix = ""
timeFormatUnixMs = "UNIXMS"
timeFormatUnixMicro = "UNIXMICRO"
timeFormatUnixNano = "UNIXNANO"
durationFormatFloat = "float"
durationFormatInt = "int"
durationFormatString = "string"
)
func appendIntegerTimestamp(dst []byte, t time.Time) []byte {
major := majorTypeTags
minor := additionalTypeTimestamp
@@ -63,17 +74,25 @@ func (e Encoder) AppendTimes(dst []byte, vals []time.Time, unused string) []byte
// AppendDuration encodes and adds a duration to the dst byte array.
// useInt field indicates whether to store the duration as seconds (integer) or
// as seconds+nanoseconds (float).
func (e Encoder) AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool, unused int) []byte {
func (e Encoder) AppendDuration(dst []byte, d time.Duration, unit time.Duration, format string, useInt bool, unused int) []byte {
if useInt {
return e.AppendInt64(dst, int64(d/unit))
}
switch format {
case durationFormatFloat:
return e.AppendFloat64(dst, float64(d)/float64(unit), unused)
case durationFormatInt:
return e.AppendInt64(dst, int64(d/unit))
case durationFormatString:
return e.AppendString(dst, d.String())
}
return e.AppendFloat64(dst, float64(d)/float64(unit), unused)
}
// AppendDurations encodes and adds an array of durations to the dst byte array.
// useInt field indicates whether to store the duration as seconds (integer) or
// as seconds+nanoseconds (float).
func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool, unused int) []byte {
func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, format string, useInt bool, unused int) []byte {
major := majorTypeArray
l := len(vals)
if l == 0 {
@@ -86,7 +105,7 @@ func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Dur
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, d := range vals {
dst = e.AppendDuration(dst, d, unit, useInt, unused)
dst = e.AppendDuration(dst, d, unit, format, useInt, unused)
}
return dst
}
+144 -5
View File
@@ -5,12 +5,151 @@ import (
"encoding/hex"
"fmt"
"math"
"reflect"
"testing"
"time"
"github.com/rs/zerolog/internal"
)
func TestEncoder_AppendDuration(t *testing.T) {
type args struct {
dst []byte
d time.Duration
unit time.Duration
format string
useInt bool
unused int
}
tests := []struct {
name string
args args
want []byte
}{
{
name: "useInt",
args: args{
d: 1234567890,
unit: time.Second,
useInt: true,
},
want: []byte{1},
},
{
name: "formatFloat",
args: args{
d: 1234567890,
unit: time.Second,
format: durationFormatFloat,
},
want: []byte{251, 63, 243, 192, 202, 66, 131, 222, 27},
},
{
name: "formatInt",
args: args{
d: 1234567890,
unit: time.Second,
format: durationFormatInt,
},
want: []byte{1},
},
{
name: "formatString",
args: args{
d: 1234567890,
unit: time.Second,
format: durationFormatString,
},
want: []byte{107, 49, 46, 50, 51, 52, 53, 54, 55, 56, 57, 115},
},
{
name: "formatBlank",
args: args{
d: 1234567890,
unit: time.Second,
},
want: []byte{251, 63, 243, 192, 202, 66, 131, 222, 27},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := Encoder{}
if got := e.AppendDuration(tt.args.dst, tt.args.d, tt.args.unit, tt.args.format, tt.args.useInt, tt.args.unused); !reflect.DeepEqual(got, tt.want) {
t.Errorf("AppendDuration() = %v, want %v", got, tt.want)
}
})
}
}
func TestEncoder_AppendDurations(t *testing.T) {
type args struct {
dst []byte
vals []time.Duration
unit time.Duration
format string
useInt bool
unused int
}
tests := []struct {
name string
args args
want []byte
}{
{
name: "useInt",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
useInt: true,
},
want: []byte{129, 1},
},
{
name: "formatFloat",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
format: durationFormatFloat,
},
want: []byte{129, 251, 63, 243, 192, 202, 66, 131, 222, 27},
},
{
name: "formatInt",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
format: durationFormatInt,
},
want: []byte{129, 1},
},
{
name: "formatString",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
format: durationFormatString,
},
want: []byte{129, 107, 49, 46, 50, 51, 52, 53, 54, 55, 56, 57, 115},
},
{
name: "formatBlank",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
},
want: []byte{129, 251, 63, 243, 192, 202, 66, 131, 222, 27},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := Encoder{}
if got := e.AppendDurations(tt.args.dst, tt.args.vals, tt.args.unit, tt.args.format, tt.args.useInt, tt.args.unused); !reflect.DeepEqual(got, tt.want) {
t.Errorf("AppendDurations() = %v, want %v", got, tt.want)
}
})
}
}
func TestAppendTimeNow(t *testing.T) {
tm := time.Now()
s := enc.AppendTime([]byte{}, tm, "unused")
@@ -116,7 +255,7 @@ func TestAppendDurationFloat(t *testing.T) {
dur := tt.Duration
want := []byte{}
want = append(want, []byte(tt.FloatOut)...)
got := enc.AppendDuration([]byte{}, dur, time.Microsecond, false, -1)
got := enc.AppendDuration([]byte{}, dur, time.Microsecond, "", false, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDuration(%v)=\ngot: 0x%s\nwant: 0x%s",
dur,
@@ -130,7 +269,7 @@ func TestAppendDurationInteger(t *testing.T) {
dur := tt.Duration
want := []byte{}
want = append(want, []byte(tt.IntegerOut)...)
got := enc.AppendDuration([]byte{}, dur, time.Microsecond, true, -1)
got := enc.AppendDuration([]byte{}, dur, time.Microsecond, "", true, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDuration(%v)=\ngot: 0x%s\nwant: 0x%s",
dur,
@@ -148,7 +287,7 @@ func TestAppendDurations(t *testing.T) {
want = append(want, []byte(tt.FloatOut)...)
}
got := enc.AppendDurations([]byte{}, array, time.Microsecond, false, -1)
got := enc.AppendDurations([]byte{}, array, time.Microsecond, "", false, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDurations(%v)\ngot: 0x%s\nwant: 0x%s",
array, hex.EncodeToString(got),
@@ -160,7 +299,7 @@ func TestAppendDurations(t *testing.T) {
want = make([]byte, 0)
want = append(want, 0x9f) // start and end array
want = append(want, 0xff) // for empty array
got = enc.AppendDurations([]byte{}, array, time.Microsecond, false, -1)
got = enc.AppendDurations([]byte{}, array, time.Microsecond, "", false, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDurations(%v)\ngot: 0x%s\nwant: 0x%s",
array, hex.EncodeToString(got),
@@ -178,7 +317,7 @@ func TestAppendDurations(t *testing.T) {
array[i] = testtime
want = append(want, []byte(outbytes)...)
}
got = enc.AppendDurations([]byte{}, array, time.Microsecond, false, -1)
got = enc.AppendDurations([]byte{}, array, time.Microsecond, "", false, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDurations(%v)\ngot: 0x%s\nwant: 0x%s",
array,
+19 -8
View File
@@ -7,10 +7,13 @@ import (
const (
// Import from zerolog/global.go
timeFormatUnix = ""
timeFormatUnixMs = "UNIXMS"
timeFormatUnixMicro = "UNIXMICRO"
timeFormatUnixNano = "UNIXNANO"
timeFormatUnix = ""
timeFormatUnixMs = "UNIXMS"
timeFormatUnixMicro = "UNIXMICRO"
timeFormatUnixNano = "UNIXNANO"
durationFormatFloat = "float"
durationFormatInt = "int"
durationFormatString = "string"
)
// AppendTime formats the input time with the given format
@@ -88,24 +91,32 @@ func appendUnixNanoTimes(dst []byte, vals []time.Time, div int64) []byte {
// AppendDuration formats the input duration with the given unit & format
// and appends the encoded string to the input byte slice.
func (e Encoder) AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool, precision int) []byte {
func (e Encoder) AppendDuration(dst []byte, d time.Duration, unit time.Duration, format string, useInt bool, precision int) []byte {
if useInt {
return strconv.AppendInt(dst, int64(d/unit), 10)
}
switch format {
case durationFormatFloat:
return e.AppendFloat64(dst, float64(d)/float64(unit), precision)
case durationFormatInt:
return e.AppendInt64(dst, int64(d/unit))
case durationFormatString:
return e.AppendString(dst, d.String())
}
return e.AppendFloat64(dst, float64(d)/float64(unit), precision)
}
// AppendDurations formats the input durations with the given unit & format
// and appends the encoded string list to the input byte slice.
func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool, precision int) []byte {
func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, format string, useInt bool, precision int) []byte {
if len(vals) == 0 {
return append(dst, '[', ']')
}
dst = append(dst, '[')
dst = e.AppendDuration(dst, vals[0], unit, useInt, precision)
dst = e.AppendDuration(dst, vals[0], unit, format, useInt, precision)
if len(vals) > 1 {
for _, d := range vals[1:] {
dst = e.AppendDuration(append(dst, ','), d, unit, useInt, precision)
dst = e.AppendDuration(append(dst, ','), d, unit, format, useInt, precision)
}
}
dst = append(dst, ']')
+144 -5
View File
@@ -3,12 +3,151 @@ package json
import (
"bytes"
"fmt"
"reflect"
"testing"
"time"
"github.com/rs/zerolog/internal"
)
func TestEncoder_AppendDuration(t *testing.T) {
type args struct {
dst []byte
d time.Duration
unit time.Duration
format string
useInt bool
unused int
}
tests := []struct {
name string
args args
want []byte
}{
{
name: "useInt",
args: args{
d: 1234567890,
unit: time.Second,
useInt: true,
},
want: []byte{49},
},
{
name: "formatFloat",
args: args{
d: 1234567890,
unit: time.Second,
format: durationFormatFloat,
},
want: []byte{49},
},
{
name: "formatInt",
args: args{
d: 1234567890,
unit: time.Second,
format: durationFormatInt,
},
want: []byte{49},
},
{
name: "formatString",
args: args{
d: 1234567890,
unit: time.Second,
format: durationFormatString,
},
want: []byte{34, 49, 46, 50, 51, 52, 53, 54, 55, 56, 57, 115, 34},
},
{
name: "formatBlank",
args: args{
d: 1234567890,
unit: time.Second,
},
want: []byte{49},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := Encoder{}
if got := e.AppendDuration(tt.args.dst, tt.args.d, tt.args.unit, tt.args.format, tt.args.useInt, tt.args.unused); !reflect.DeepEqual(got, tt.want) {
t.Errorf("AppendDuration() = %v, want %v", got, tt.want)
}
})
}
}
func TestEncoder_AppendDurations(t *testing.T) {
type args struct {
dst []byte
vals []time.Duration
unit time.Duration
format string
useInt bool
unused int
}
tests := []struct {
name string
args args
want []byte
}{
{
name: "useInt",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
useInt: true,
},
want: []byte{91, 49, 93},
},
{
name: "formatFloat",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
format: durationFormatFloat,
},
want: []byte{91, 49, 93},
},
{
name: "formatInt",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
format: durationFormatInt,
},
want: []byte{91, 49, 93},
},
{
name: "formatString",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
format: durationFormatString,
},
want: []byte{91, 34, 49, 46, 50, 51, 52, 53, 54, 55, 56, 57, 115, 34, 93},
},
{
name: "formatBlank",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
},
want: []byte{91, 49, 93},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := Encoder{}
if got := e.AppendDurations(tt.args.dst, tt.args.vals, tt.args.unit, tt.args.format, tt.args.useInt, tt.args.unused); !reflect.DeepEqual(got, tt.want) {
t.Errorf("AppendDurations() = %v, want %v", got, tt.want)
}
})
}
}
func TestAppendTimeNow(t *testing.T) {
tm := time.Now()
got := enc.AppendTime([]byte{}, tm, time.RFC3339)
@@ -141,7 +280,7 @@ func TestAppendDurationFloat(t *testing.T) {
dur := tt.Duration
want := []byte{}
want = append(want, []byte(fmt.Sprintf("%v", dur.Microseconds()))...)
got := enc.AppendDuration([]byte{}, dur, time.Microsecond, false, -1)
got := enc.AppendDuration([]byte{}, dur, time.Microsecond, "", false, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDuration(%v)=\ngot: %s\nwant: %s",
dur,
@@ -152,7 +291,7 @@ func TestAppendDurationFloat(t *testing.T) {
want = []byte{}
fraction := float64(dur) / float64(time.Millisecond)
want = append(want, []byte(fmt.Sprintf("%v", fraction))...)
got = enc.AppendDuration([]byte{}, dur, time.Millisecond, false, -1)
got = enc.AppendDuration([]byte{}, dur, time.Millisecond, "", false, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDuration(%v)=\ngot: %s\nwant: %s",
dur,
@@ -167,7 +306,7 @@ func TestAppendDurationInteger(t *testing.T) {
want := []byte{}
whole := int(dur) / int(time.Microsecond)
want = append(want, []byte(fmt.Sprintf("%v", whole))...)
got := enc.AppendDuration([]byte{}, dur, time.Microsecond, true, -1)
got := enc.AppendDuration([]byte{}, dur, time.Microsecond, "", true, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDuration(%v)=\ngot: %s\nwant: %s",
dur,
@@ -190,7 +329,7 @@ func TestAppendDurations(t *testing.T) {
}
want = append(want, ']')
got := enc.AppendDurations([]byte{}, array, time.Microsecond, false, -1)
got := enc.AppendDurations([]byte{}, array, time.Microsecond, "", false, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDurations(%v)\ngot: %s\nwant: %s",
array,
@@ -203,7 +342,7 @@ func TestAppendDurations(t *testing.T) {
want = make([]byte, 0)
want = append(want, '[')
want = append(want, ']')
got = enc.AppendDurations([]byte{}, array, time.Microsecond, false, -1)
got = enc.AppendDurations([]byte{}, array, time.Microsecond, "", false, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDurations(%v)\ngot: %s\nwant: %s",
array,