Skip to content

Commit

Permalink
Encode time in structs (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
outofforest authored Jul 26, 2024
1 parent 6227033 commit d4b4b89
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 7 additions & 2 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,13 @@ func (c *console) AppendReflected(value interface{}) error {
return c.AppendReflected(v.Elem().Interface())
}
case reflect.Struct:
c.buffer.AppendByte('\n')
return c.appendReflectedStruct(v)
switch v.Type() {
case reflect.TypeOf(time.Time{}):
c.AppendTime(v.Interface().(time.Time))
default:
c.buffer.AppendByte('\n')
return c.appendReflectedStruct(v)
}
case reflect.String:
c.AppendString(v.String())
default:
Expand Down
5 changes: 3 additions & 2 deletions print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func (a array) MarshalLogArray(marshaler zapcore.ArrayEncoder) error {

type object struct {
Field1 string
Field2 int
Field2 time.Time
Field3 int
}

type object2 struct {
Expand Down Expand Up @@ -54,7 +55,7 @@ func TestPrint(t *testing.T) {
zap.Int("withField2", 2))
log.Error("This is error, it contains error field with stack, so log stack trace should not be generated",
zap.Array("array", array{0, 1, 2, 3, 4}),
zap.Any("any", object{Field1: "stringValue", Field2: 3}),
zap.Any("any", object{Field1: "stringValue", Field2: time.Now(), Field3: 3}),
zap.Any("nil", nil),
zap.Bool("bool", false),
zap.Bools("bools", []bool{true, false, true}),
Expand Down

0 comments on commit d4b4b89

Please sign in to comment.