mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
lib/logger: support for renaming json fields (#3488)
This commit is contained in:
parent
56a9ea3753
commit
72f8fce107
2 changed files with 51 additions and 6 deletions
32
lib/logger/json-fields.go
Normal file
32
lib/logger/json-fields.go
Normal file
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue