Skip to content

adjust time format in logs to UTC #283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ var (
dataDir string
logDir string // Custom log directory (default "")
logOutput struct {
Color bool
Console bool
File bool
Color bool
Console bool
File bool
TimeFormat string
}
ownAddress string
bindAddress string
Expand Down Expand Up @@ -205,6 +206,8 @@ func init() {
pf.BoolVar(&logOutput.Console, "log.console", true, "Send log output to console")
pf.BoolVar(&logOutput.File, "log.file", true, "Send log output to file")
pf.BoolVar(&logOutput.Color, "log.color", defaultLogColor, "Colorize the log output")
pf.StringVar(&logOutput.TimeFormat, "log.time-format", "local-datestring",
"Time format to use in logs. Possible values: 'local-datestring' (default), 'utc-datestring'")
pf.StringVar(&logDir, "log.dir", getEnvVar("LOG_DIR", ""), "Custom log file directory.")
f.IntVar(&logRotateFilesToKeep, "log.rotate-files-to-keep", defaultLogRotateFilesToKeep, "Number of files to keep when rotating log files")
f.DurationVar(&logRotateInterval, "log.rotate-interval", defaultLogRotateInterval, "Time between log rotations (0 disables log rotation)")
Expand Down Expand Up @@ -514,9 +517,15 @@ func cmdMainRun(cmd *cobra.Command, args []string) {
// configureLogging configures the log object according to command line arguments.
func configureLogging(consoleOnly bool) {
logOpts := logging.LoggerOutputOptions{
Stderr: logOutput.Console,
Color: logOutput.Color,
Stderr: logOutput.Console,
Color: logOutput.Color,
TimeFormat: logging.TimeFormatLocal,
}

if logOutput.TimeFormat == "utc-datestring" {
logOpts.TimeFormat = logging.TimeFormatUTC
}

if logOutput.File && !consoleOnly {
if logDir != "" {
logOpts.LogFile = filepath.Join(logDir, logFileName)
Expand Down
47 changes: 28 additions & 19 deletions pkg/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ import (
"os"
"strings"
"sync"
"time"

"github.com/rs/zerolog"
)

type TimeFormat int

const (
TimeFormatLocal TimeFormat = 0
TimeFormatUTC TimeFormat = 1
)

var (
// The defaultLevels list is used during development to increase the
// default level for components that we care a little less about.
Expand Down Expand Up @@ -72,14 +80,14 @@ type loggingService struct {
}

type LoggerOutputOptions struct {
Color bool // Produce colored logs
JSON bool // Project JSON messages
Stderr bool // Write logs to stderr
LogFile string // Path of file to write to
Color bool // Produce colored logs
TimeFormat TimeFormat // Instructs how to print time in logs
Stderr bool // Write logs to stderr
LogFile string // Path of file to write to
}

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

lg.FormatLevel = func(i interface{}) string {
return fmt.Sprintf("|%s|", strings.ToUpper(fmt.Sprintf("%s", i)))
Expand All @@ -93,31 +101,32 @@ func NewRootLogger(options LoggerOutputOptions) (zerolog.Logger, func()) {
var writers []io.Writer
var errors []error
var rotate func()

if options.TimeFormat == TimeFormatUTC {
zerolog.TimestampFunc = func() time.Time {
return time.Now().UTC()
}
}

if options.LogFile != "" {
fileWriter, err := newRotatingWriter(options.LogFile)
if err != nil {
errors = append(errors, err)
options.Stderr = true
} else {
rotate = func() { fileWriter.Rotate() }
writer := io.Writer(fileWriter)
if !options.JSON {
writer = configureLogger(zerolog.ConsoleWriter{
Out: fileWriter,
NoColor: true,
})
}
writer := configureLogger(zerolog.ConsoleWriter{
Out: fileWriter,
NoColor: true,
})
writers = append(writers, writer)
}
}
if options.Stderr {
writer := io.Writer(os.Stderr)
if !options.JSON {
writer = configureLogger(zerolog.ConsoleWriter{
Out: writer,
NoColor: !options.Color,
})
}
writer := configureLogger(zerolog.ConsoleWriter{
Out: os.Stderr,
NoColor: !options.Color,
})
writers = append(writers, writer)
}

Expand Down