From 0df4f7dd2431aadeefac57da05e14c33f4c13e75 Mon Sep 17 00:00:00 2001 From: Igor Postelnik Date: Thu, 21 Jan 2021 14:30:02 -0600 Subject: [PATCH] TUN-3795: Use RFC-3339 style date format for logs, produce timestamp in UTC --- logger/create.go | 13 ++++++++++++- origin/backoffhandler_test.go | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/logger/create.go b/logger/create.go index 400aea49..4a6112b4 100644 --- a/logger/create.go +++ b/logger/create.go @@ -7,6 +7,7 @@ import ( "path" "path/filepath" "sync" + "time" "github.com/mattn/go-colorable" "github.com/rs/zerolog" @@ -30,8 +31,18 @@ const ( dirPermMode = 0744 // rwxr--r-- filePermMode = 0644 // rw-r--r-- + + consoleTimeFormat = time.RFC3339 ) +func init() { + zerolog.TimestampFunc = utcNow +} + +func utcNow() time.Time { + return time.Now().UTC() +} + func fallbackLogger(err error) *zerolog.Logger { failLog := fallbacklog.With().Logger() fallbacklog.Error().Msgf("Falling back to a default logger due to logger setup failure: %s", err) @@ -53,7 +64,6 @@ func (t resilientMultiWriter) Write(p []byte) (n int, err error) { return len(p), nil } - func newZerolog(loggerConfig *Config) *zerolog.Logger { var writers []io.Writer @@ -143,6 +153,7 @@ func createConsoleLogger(config ConsoleConfig) io.Writer { return zerolog.ConsoleWriter{ Out: colorable.NewColorable(consoleOut), NoColor: config.noColor || !term.IsTerminal(int(consoleOut.Fd())), + TimeFormat: consoleTimeFormat, } } diff --git a/origin/backoffhandler_test.go b/origin/backoffhandler_test.go index 6c8e4e9c..b588e41d 100644 --- a/origin/backoffhandler_test.go +++ b/origin/backoffhandler_test.go @@ -2,6 +2,7 @@ package origin import ( "context" + "fmt" "testing" "time" ) @@ -145,3 +146,16 @@ func TestBackoffRetryForever(t *testing.T) { t.Fatalf("backoff returned %v instead of 8 seconds on fifth retry", duration) } } + +func TestPrint(t *testing.T) { + local := time.Now() + fmt.Printf("Local = %s\n", local) + fmt.Printf("Local (kitchen) = %s\n", local.Format(time.Kitchen)) + fmt.Printf("Local (RFC3339) = %s\n", local.Format(time.RFC3339)) + fmt.Println() + utc := local.UTC() + fmt.Printf("UTC = %s\n", utc) + fmt.Printf("UTC (kitchen) = %s\n", utc.Format(time.Kitchen)) + fmt.Printf("UTC (RFC3339) = %s\n", utc.Format(time.RFC3339)) + +} \ No newline at end of file