logging: extract command fields, add default fields for logged commands

This commit is contained in:
in-void 2023-01-30 14:46:59 +01:00
parent de423cc927
commit 4ee79b5fe4

View File

@ -77,16 +77,23 @@ func LogCommand(command interface{}) {
commandName := reflect.TypeOf(command).Elem().Name() commandName := reflect.TypeOf(command).Elem().Name()
customLogger.WithFields(logrus.Fields{ var commandAsFields logrus.Fields
json.Unmarshal(context, &commandAsFields)
customLogger.
WithFields(defaultFields).
WithFields(logrus.Fields{
"command_name": commandName, "command_name": commandName,
"command": string(context), "command": commandAsFields,
}).Info("Start command " + commandName) }).Info("Start command " + commandName)
} }
func LogCommandFailed(command interface{}, err error) { func LogCommandFailed(command interface{}, err error) {
commandName := reflect.TypeOf(command).Elem().Name() commandName := reflect.TypeOf(command).Elem().Name()
customLogger.WithFields(logrus.Fields{ customLogger.
WithFields(defaultFields).
WithFields(logrus.Fields{
"reason": err.Error(), "reason": err.Error(),
}).Info("Command failed" + commandName) }).Info("Command failed" + commandName)
} }