VictoriaMetrics/lib/logger/json_fields.go
Aliaksandr Valialkin 70c720d640
lib/logger: follow-up for 72f8fce107
- 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
2022-12-16 17:42:31 -08:00

46 lines
1 KiB
Go

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