I'm using logrus for logging in golang. I use the following statements for logging.
logger.Info("")
logger.Errorf("Error message")
My requirement is to have a custom field (severity) only in Errorf statements with the following constraints.
- If it is specified in the log statement (Ex:
logrus.WithField("severity", "critical").Errorf("Error message")) it should print the specified value as below.
ERRO[0000] Error message severity=critical
- If it is not specified, it should print a default value. For Example this(
logger.Errorf("Error message")) log statement should print the follows.
ERRO[0000] Error message severity=normal
Note : This should happen only for Errorf statements, which means others should works as normal.
Can someone suggest me a way to achieve this??
Write custom
Hookthat will check if entry hasseverityfield set and if not insert default value. Attach that hook to either default global or your own logger.You can limit hook to fire only on entries on
logrus.ErrorLevelby including only that level in return value ofHook.Levels():