From 72f8fce10701ae218f14d2df60a4c18211b0cf0c Mon Sep 17 00:00:00 2001 From: Michal Kralik Date: Sat, 17 Dec 2022 02:26:32 +0100 Subject: [PATCH] lib/logger: support for renaming json fields (#3488) --- lib/logger/json-fields.go | 32 ++++++++++++++++++++++++++++++++ lib/logger/logger.go | 25 +++++++++++++++++++------ 2 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 lib/logger/json-fields.go diff --git a/lib/logger/json-fields.go b/lib/logger/json-fields.go new file mode 100644 index 000000000..6ce5be497 --- /dev/null +++ b/lib/logger/json-fields.go @@ -0,0 +1,32 @@ +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/logger.go b/lib/logger/logger.go index 840cf8bf1..83938fce7 100644 --- a/lib/logger/logger.go +++ b/lib/logger/logger.go @@ -17,10 +17,11 @@ 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") - 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") + 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. "+ "For example: America/New_York, Europe/Berlin, Etc/GMT+3 or Local") disableTimestamps = flag.Bool("loggerDisableTimestamps", false, "Whether to disable writing timestamps in logs") @@ -34,6 +35,7 @@ var ( // // There is no need in calling Init from tests. func Init() { + setLoggerJSONFields() setLoggerOutput() validateLoggerLevel() validateLoggerFormat() @@ -239,9 +241,20 @@ func logMessage(level, msg string, skipframes int) { switch *loggerFormat { case "json": if *disableTimestamps { - logMsg = fmt.Sprintf(`{"level":%q,"caller":%q,"msg":%q}`+"\n", levelLowercase, location, msg) + logMsg = fmt.Sprintf( + `{%q:%q,%q:%q,%q:%q}`+"\n", + fieldLevel, levelLowercase, + fieldCaller, location, + fieldMsg, msg, + ) } else { - logMsg = fmt.Sprintf(`{"ts":%q,"level":%q,"caller":%q,"msg":%q}`+"\n", timestamp, levelLowercase, location, msg) + logMsg = fmt.Sprintf( + `{%q:%q,%q:%q,%q:%q,%q:%q}`+"\n", + fieldTs, timestamp, + fieldLevel, levelLowercase, + fieldCaller, location, + fieldMsg, msg, + ) } default: if *disableTimestamps {