TUN-3795: Use RFC-3339 style date format for logs, produce timestamp in UTC

This commit is contained in:
Igor Postelnik 2021-01-21 14:30:02 -06:00
parent 7df3a1ab67
commit 0df4f7dd24
2 changed files with 26 additions and 1 deletions

View File

@ -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,
}
}

View File

@ -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))
}