Skip to content

Commit

Permalink
Use RFC3339 in UTC as the default time format for log messages
Browse files Browse the repository at this point in the history
This ensures that times that are logged in messages can be deserialised
by the encoding/json package irrespective of the timezone that the time
is in when logged; currently if the time is not in UTC, the ISO8601
format that is used is not accepted. Also provide a non-UTC formatter
that can be used where the timezone is important.
  • Loading branch information
efd6 committed Nov 17, 2024
1 parent e1c68ae commit 084b072
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Changelog for ecszap

## unreleased
* Use RFC3339 in UTC as the default time format for log messages [pull#84](https://github.com/elastic/ecs-logging-go-zap/pull/84)

## 1.0.2
* Updated zap to v1.24.0 [pull#50](https://github.com/elastic/ecs-logging-go-zap/pull/50)
Expand Down
34 changes: 34 additions & 0 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,40 @@ func ShortCallerEncoder(c zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder
encodeCaller(&caller{c, false}, enc)
}

// RFC3339TimeEncoder serializes a time.Time to an RFC3339-formatted string
// with millisecond precision.
//
// If enc supports AppendTimeLayout(t time.Time,layout string), it's used
// instead of appending a pre-formatted string value.
func RFC3339TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
t = t.In(time.UTC)
type appendTimeEncoder interface {
AppendTimeLayout(time.Time, string)
}
// Use a custom RFC3339 layout with obligatory millisecond
// precision rather than the second-resolution or optional
// nano-second resolution that is provided in the time
// package.
const rfc3339millis = "2006-01-02T15:04:05.000Z07:00"
if enc, ok := enc.(appendTimeEncoder); ok {
enc.AppendTimeLayout(t, rfc3339millis)
return
}

enc.AppendString(t.Format(rfc3339millis))
}

// RFC3339UTCTimeEncoder serializes a time.Time to an RFC3339-formatted string
// with millisecond precision in UTC.
//
// If enc supports AppendTimeLayout(t time.Time,layout string), it's used
// instead of appending a pre-formatted string value.
//
// RFC3339UTCTimeEncoder is the default time encoder used by EncoderConfig.
func RFC3339UTCTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
RFC3339TimeEncoder(t.In(time.UTC), enc)
}

// UnmarshalText creates a CallerEncoder function,
// `full` is unmarshalled to FullCallerEncoder,
// defaults to ShortCallerEncoder,
Expand Down
2 changes: 1 addition & 1 deletion encoder_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (
messageKey = "message"
stackTraceKey = "log.origin.stack_trace"
timeKey = "@timestamp"
encodeTime = zapcore.ISO8601TimeEncoder
encodeTime = RFC3339UTCTimeEncoder
)

// EncoderConfig exports all non ECS related configurable settings.
Expand Down

0 comments on commit 084b072

Please sign in to comment.