lib/logger: add -loggerTimezone command-line flag for adjusting timezone for timestamps in log messages

This commit is contained in:
Aliaksandr Valialkin 2021-01-26 22:51:31 +02:00
parent 5481906db6
commit 3fe848cdd7
2 changed files with 16 additions and 1 deletions

View File

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

View File

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