Skip to content

Commit 31ffffa

Browse files
authored
Adjust time format in logs to UTC (#283)
1 parent 9dd6555 commit 31ffffa

File tree

2 files changed

+42
-24
lines changed

2 files changed

+42
-24
lines changed

main.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ var (
110110
dataDir string
111111
logDir string // Custom log directory (default "")
112112
logOutput struct {
113-
Color bool
114-
Console bool
115-
File bool
113+
Color bool
114+
Console bool
115+
File bool
116+
TimeFormat string
116117
}
117118
ownAddress string
118119
bindAddress string
@@ -205,6 +206,8 @@ func init() {
205206
pf.BoolVar(&logOutput.Console, "log.console", true, "Send log output to console")
206207
pf.BoolVar(&logOutput.File, "log.file", true, "Send log output to file")
207208
pf.BoolVar(&logOutput.Color, "log.color", defaultLogColor, "Colorize the log output")
209+
pf.StringVar(&logOutput.TimeFormat, "log.time-format", "local-datestring",
210+
"Time format to use in logs. Possible values: 'local-datestring' (default), 'utc-datestring'")
208211
pf.StringVar(&logDir, "log.dir", getEnvVar("LOG_DIR", ""), "Custom log file directory.")
209212
f.IntVar(&logRotateFilesToKeep, "log.rotate-files-to-keep", defaultLogRotateFilesToKeep, "Number of files to keep when rotating log files")
210213
f.DurationVar(&logRotateInterval, "log.rotate-interval", defaultLogRotateInterval, "Time between log rotations (0 disables log rotation)")
@@ -514,9 +517,15 @@ func cmdMainRun(cmd *cobra.Command, args []string) {
514517
// configureLogging configures the log object according to command line arguments.
515518
func configureLogging(consoleOnly bool) {
516519
logOpts := logging.LoggerOutputOptions{
517-
Stderr: logOutput.Console,
518-
Color: logOutput.Color,
520+
Stderr: logOutput.Console,
521+
Color: logOutput.Color,
522+
TimeFormat: logging.TimeFormatLocal,
519523
}
524+
525+
if logOutput.TimeFormat == "utc-datestring" {
526+
logOpts.TimeFormat = logging.TimeFormatUTC
527+
}
528+
520529
if logOutput.File && !consoleOnly {
521530
if logDir != "" {
522531
logOpts.LogFile = filepath.Join(logDir, logFileName)

pkg/logging/logger.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,18 @@ import (
2929
"os"
3030
"strings"
3131
"sync"
32+
"time"
3233

3334
"github.com/rs/zerolog"
3435
)
3536

37+
type TimeFormat int
38+
39+
const (
40+
TimeFormatLocal TimeFormat = 0
41+
TimeFormatUTC TimeFormat = 1
42+
)
43+
3644
var (
3745
// The defaultLevels list is used during development to increase the
3846
// default level for components that we care a little less about.
@@ -72,14 +80,14 @@ type loggingService struct {
7280
}
7381

7482
type LoggerOutputOptions struct {
75-
Color bool // Produce colored logs
76-
JSON bool // Project JSON messages
77-
Stderr bool // Write logs to stderr
78-
LogFile string // Path of file to write to
83+
Color bool // Produce colored logs
84+
TimeFormat TimeFormat // Instructs how to print time in logs
85+
Stderr bool // Write logs to stderr
86+
LogFile string // Path of file to write to
7987
}
8088

8189
func configureLogger(lg zerolog.ConsoleWriter) zerolog.ConsoleWriter {
82-
lg.TimeFormat = "2006-01-02T15:04:05-07:00"
90+
lg.TimeFormat = time.RFC3339
8391

8492
lg.FormatLevel = func(i interface{}) string {
8593
return fmt.Sprintf("|%s|", strings.ToUpper(fmt.Sprintf("%s", i)))
@@ -93,31 +101,32 @@ func NewRootLogger(options LoggerOutputOptions) (zerolog.Logger, func()) {
93101
var writers []io.Writer
94102
var errors []error
95103
var rotate func()
104+
105+
if options.TimeFormat == TimeFormatUTC {
106+
zerolog.TimestampFunc = func() time.Time {
107+
return time.Now().UTC()
108+
}
109+
}
110+
96111
if options.LogFile != "" {
97112
fileWriter, err := newRotatingWriter(options.LogFile)
98113
if err != nil {
99114
errors = append(errors, err)
100115
options.Stderr = true
101116
} else {
102117
rotate = func() { fileWriter.Rotate() }
103-
writer := io.Writer(fileWriter)
104-
if !options.JSON {
105-
writer = configureLogger(zerolog.ConsoleWriter{
106-
Out: fileWriter,
107-
NoColor: true,
108-
})
109-
}
118+
writer := configureLogger(zerolog.ConsoleWriter{
119+
Out: fileWriter,
120+
NoColor: true,
121+
})
110122
writers = append(writers, writer)
111123
}
112124
}
113125
if options.Stderr {
114-
writer := io.Writer(os.Stderr)
115-
if !options.JSON {
116-
writer = configureLogger(zerolog.ConsoleWriter{
117-
Out: writer,
118-
NoColor: !options.Color,
119-
})
120-
}
126+
writer := configureLogger(zerolog.ConsoleWriter{
127+
Out: os.Stderr,
128+
NoColor: !options.Color,
129+
})
121130
writers = append(writers, writer)
122131
}
123132

0 commit comments

Comments
 (0)