From 3fe848cdd73f273ba7725f42953f96e9db40c22c Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 26 Jan 2021 22:51:31 +0200 Subject: [PATCH] lib/logger: add `-loggerTimezone` command-line flag for adjusting timezone for timestamps in log messages --- docs/CHANGELOG.md | 1 + lib/logger/logger.go | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index e2039ccf2..fae2dff95 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -2,6 +2,7 @@ # tip +* FEATURE: added `-loggerTimezone` command-line flag for adjusting time zone for timestamps in log messages. By default UTC is used. * FEATURE: added `-search.maxStepForPointsAdjustment` command-line flag, which can be used for disabling adjustment for points returned `/api/v1/query_range` handler if such points have timestamps closer than `-search.latencyOffset` to the current time. Such points may contain incomplete data, so they are substituted by the previous values for `step` query args smaller than one minute by default. * FEATURE: vmalert: added `-datasource.queryStep` command-line flag for passing `step` query arg to `/api/v1/query` endpoint. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1025 diff --git a/lib/logger/logger.go b/lib/logger/logger.go index 0e64ef332..ed65e7dfe 100644 --- a/lib/logger/logger.go +++ b/lib/logger/logger.go @@ -20,6 +20,7 @@ var ( loggerLevel = flag.String("loggerLevel", "INFO", "Minimum level of errors to log. Possible values: INFO, WARN, ERROR, FATAL, PANIC") loggerFormat = flag.String("loggerFormat", "default", "Format for logs. Possible values: default, json") loggerOutput = flag.String("loggerOutput", "stderr", "Output for the logs. Supported values: stderr, stdout") + loggerTimezone = flag.String("loggerTimezone", "UTC", "Timezone to use for timestamps in logs. Local timezone can be used") disableTimestamps = flag.Bool("loggerDisableTimestamps", false, "Whether to disable writing timestamps in logs") errorsPerSecondLimit = flag.Int("loggerErrorsPerSecondLimit", 0, "Per-second limit on the number of ERROR messages. If more than the given number of errors "+ @@ -37,10 +38,23 @@ func Init() { setLoggerOutput() validateLoggerLevel() validateLoggerFormat() + initTimezone() go logLimiterCleaner() logAllFlags() + } +func initTimezone() { + tz, err := time.LoadLocation(*loggerTimezone) + if err != nil { + log.Printf("cannot load timezone %q, so using UTC; error: %s", *loggerTimezone, err) + tz = time.UTC + } + timezone = tz +} + +var timezone *time.Location + func setLoggerOutput() { switch *loggerOutput { case "stderr": @@ -192,7 +206,7 @@ func (lw *logWriter) Write(p []byte) (int, error) { func logMessage(level, msg string, skipframes int) { timestamp := "" if !*disableTimestamps { - timestamp = time.Now().UTC().Format("2006-01-02T15:04:05.000Z") + timestamp = time.Now().In(timezone).Format("2006-01-02T15:04:05.000Z0700") } levelLowercase := strings.ToLower(level) _, file, line, ok := runtime.Caller(skipframes)