From b9568dba3261aefebd1eaae12a33c4b2635422b0 Mon Sep 17 00:00:00 2001 From: James Blair Date: Fri, 3 Feb 2023 21:45:45 +1300 Subject: [PATCH] Fix regression in timestamp resolution Historic capnslog timestamps are in microsecond resolution. We need to match that when we migrate to the zap logger. Signed-off-by: James Blair --- client/pkg/logutil/zap.go | 24 +++++++++++++++--------- tests/e2e/zap_logging_test.go | 2 +- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/client/pkg/logutil/zap.go b/client/pkg/logutil/zap.go index d7fd0d90d..34f35b9f2 100644 --- a/client/pkg/logutil/zap.go +++ b/client/pkg/logutil/zap.go @@ -16,6 +16,7 @@ package logutil import ( "sort" + "time" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -46,15 +47,20 @@ var DefaultZapLoggerConfig = zap.Config{ // copied from "zap.NewProductionEncoderConfig" with some updates EncoderConfig: zapcore.EncoderConfig{ - TimeKey: "ts", - LevelKey: "level", - NameKey: "logger", - CallerKey: "caller", - MessageKey: "msg", - StacktraceKey: "stacktrace", - LineEnding: zapcore.DefaultLineEnding, - EncodeLevel: zapcore.LowercaseLevelEncoder, - EncodeTime: zapcore.ISO8601TimeEncoder, + TimeKey: "ts", + LevelKey: "level", + NameKey: "logger", + CallerKey: "caller", + MessageKey: "msg", + StacktraceKey: "stacktrace", + LineEnding: zapcore.DefaultLineEnding, + EncodeLevel: zapcore.LowercaseLevelEncoder, + + // Custom EncodeTime function to ensure we match format and precision of historic capnslog timestamps + EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) { + enc.AppendString(t.Format("2006-01-02T15:04:05.999999Z0700")) + }, + EncodeDuration: zapcore.StringDurationEncoder, EncodeCaller: zapcore.ShortCallerEncoder, }, diff --git a/tests/e2e/zap_logging_test.go b/tests/e2e/zap_logging_test.go index fa20f570b..5ac768187 100644 --- a/tests/e2e/zap_logging_test.go +++ b/tests/e2e/zap_logging_test.go @@ -56,7 +56,7 @@ func TestServerJsonLogging(t *testing.T) { if entry.Timestamp == "" { t.Errorf(`Missing "ts" key, line: %s`, line) } - if _, err := time.Parse("2006-01-02T15:04:05.000Z0700", entry.Timestamp); entry.Timestamp != "" && err != nil { + if _, err := time.Parse("2006-01-02T15:04:05.999999Z0700", entry.Timestamp); entry.Timestamp != "" && err != nil { t.Errorf(`Unexpected "ts" key format, err: %s`, err) } if entry.Caller == "" {