Skip to content

Instantly share code, notes, and snippets.

@mesuutt
Created February 17, 2021 09:54
Show Gist options
  • Save mesuutt/56672ce413660f17f445a39ea49d36c8 to your computer and use it in GitHub Desktop.
Save mesuutt/56672ce413660f17f445a39ea49d36c8 to your computer and use it in GitHub Desktop.
// formatter adds default fields to each log entry. https://github.com/sirupsen/logrus/pull/653#issuecomment-454467900
type formatter struct {
Fields logrus.Fields
Lf logrus.Formatter
}
// Format satisfies the logrus.Formatter interface.
func (f *formatter) Format(e *logrus.Entry) ([]byte, error) {
for k, v := range f.Fields {
e.Data[k] = v
}
return f.Lf.Format(e)
}
// Set logrus global logger
logrus.SetFormatter(&formatter{
Fields: logrus.Fields{
"facility": "myApp",
},
Lf: &logrus.JSONFormatter{
FieldMap: logrus.FieldMap{
logrus.FieldKeyMsg: "message",
},
TimestampFormat: time.RFC3339Nano,
},
})
// logrus.AddHook(hook)
myLogger := logrus.StandardLogger()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment