From 4cb83f0f4a89b495c488f81b2c70a1aad6265101 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 16 Dec 2022 17:40:46 -0800 Subject: [PATCH] lib/logger: follow-up for 72f8fce10701ae218f14d2df60a4c18211b0cf0c - Document the change at docs/CHANELOG.md - Log fatal errors if the -loggerJSONFields contains unexpected values - Rename -loggerJsonFields to -loggerJSONFields for the sake of consistency naming commonly used in Go Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2348 --- docs/CHANGELOG.md | 1 + lib/logger/json-fields.go | 32 --------------------------- lib/logger/json_fields.go | 46 +++++++++++++++++++++++++++++++++++++++ lib/logger/logger.go | 9 ++++---- 4 files changed, 51 insertions(+), 37 deletions(-) delete mode 100644 lib/logger/json-fields.go create mode 100644 lib/logger/json_fields.go diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 89fd09be4..58dd99b35 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -16,6 +16,7 @@ The following tip changes can be tested by building VictoriaMetrics components f ## tip * FEATURE: support overriding of `-search.latencyOffset` value via URL param `latency_offset` when performing requests to [/api/v1/query](https://docs.victoriametrics.com/keyConcepts.html#instant-query) and [/api/v1/query_range](https://docs.victoriametrics.com/keyConcepts.html#range-query). See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3481). +* FEATURE: allow changing field names in JSON logs if VictoriaMetrics components are started with `-loggerFormat=json` command-line flags. The field names can be changed with the `-loggerJSONFields` command-line flag. For example `-loggerJSONFields=ts:timestamp,msg:message` would rename `ts` and `msg` fields on the output JSON to `timestamp` and `message` fields. See [this feature request](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2348). Thanks to @michal-kralik for [the pull request](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/3488). * BUGFIX: allow specifying values bigger than 2GiB to the following command-line flag values on 32-bit architectures (`386` and `arm`): `-storage.minFreeDiskSpaceBytes` and `-remoteWrite.maxDiskUsagePerURL`. Previously values bigger than 2GiB were incorrectly truncated on these architectures. * BUGFIX: [vmagent](https://docs.victoriametrics.com/vmagent.html): stop dropping metric name by a mistake on the [/metric-relabel-debug](https://docs.victoriametrics.com/vmagent.html#relabel-debug) page. diff --git a/lib/logger/json-fields.go b/lib/logger/json-fields.go deleted file mode 100644 index 6ce5be497..000000000 --- a/lib/logger/json-fields.go +++ /dev/null @@ -1,32 +0,0 @@ -package logger - -import "strings" - -var ( - fieldTs = "ts" - fieldLevel = "level" - fieldCaller = "caller" - fieldMsg = "msg" -) - -func setLoggerJSONFields() { - fields := strings.Split(*loggerJSONFields, ",") - for _, f := range fields { - v := strings.Split(strings.TrimSpace(f), ":") - if len(v) != 2 { - continue - } - - old, new := v[0], v[1] - switch old { - case "ts": - fieldTs = new - case "level": - fieldLevel = new - case "caller": - fieldCaller = new - case "msg": - fieldMsg = new - } - } -} diff --git a/lib/logger/json_fields.go b/lib/logger/json_fields.go new file mode 100644 index 000000000..d8351211e --- /dev/null +++ b/lib/logger/json_fields.go @@ -0,0 +1,46 @@ +package logger + +import ( + "flag" + "log" + "strings" +) + +var loggerJSONFields = flag.String("loggerJSONFields", "", `Allows renaming fields in JSON formatted logs. `+ + `Example: "ts:timestamp,msg:message" renames "ts" to "timestamp" and "msg" to "message". `+ + "Supported fields: ts, level, caller, msg") + +func setLoggerJSONFields() { + if *loggerJSONFields == "" { + return + } + fields := strings.Split(*loggerJSONFields, ",") + for _, f := range fields { + f = strings.TrimSpace(f) + v := strings.Split(f, ":") + if len(v) != 2 { + log.Fatalf("missing ':' delimiter in -loggerJSONFields=%q for %q item", *loggerJSONFields, f) + } + + name, value := v[0], v[1] + switch name { + case "ts": + fieldTs = value + case "level": + fieldLevel = value + case "caller": + fieldCaller = value + case "msg": + fieldMsg = value + default: + log.Fatalf("unexpected json field name in -loggerJSONFields=%q: %q; supported values: ts, level, caller, msg", *loggerJSONFields, name) + } + } +} + +var ( + fieldTs = "ts" + fieldLevel = "level" + fieldCaller = "caller" + fieldMsg = "msg" +) diff --git a/lib/logger/logger.go b/lib/logger/logger.go index 83938fce7..97539b29e 100644 --- a/lib/logger/logger.go +++ b/lib/logger/logger.go @@ -17,11 +17,10 @@ import ( ) 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") - loggerJSONFields = flag.String("loggerJsonFields", "", `Allows renaming fields in JSON formatted logs. Example: "ts:timestamp,msg:message" renames "ts" to "timestamp" and "msg" to "message"`) - loggerTimezone = flag.String("loggerTimezone", "UTC", "Timezone to use for timestamps in logs. Timezone must be a valid IANA Time Zone. "+ + 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. Timezone must be a valid IANA Time Zone. "+ "For example: America/New_York, Europe/Berlin, Etc/GMT+3 or Local") disableTimestamps = flag.Bool("loggerDisableTimestamps", false, "Whether to disable writing timestamps in logs")