diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 89fd09be47..58dd99b357 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 new file mode 100644 index 0000000000..d8351211e7 --- /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" +)