I use logrus in all my go apps and recently I started using a context logger. Now I want to "build up" a context during the execution path of my app. See example below, which illustrates what I want.
package main
import (
"github.com/Sirupsen/logrus"
)
func main() {
logrus.Info("normal logger")
cl := logrus.WithFields(
logrus.Fields{
"extra_field_one": "extra_value_one",
})
// some code here
// here I want to add an additional field to to contextlogger cl.
// How do I do that?
}
EDIT
As ymonad mentioned, it's possible by overwriting the contextLogger. Also found out that you can add one additional field:
cl = cl.WithField("key", "value")
You can just call cl.WithFields()
package main
import "github.com/Sirupsen/logrus"
func main() {
cl := logrus.WithFields(
logrus.Fields{
"extra_field_one": "extra_value_one",
})
cl = cl.WithFields(
logrus.Fields{
"extra_field_two": "extra_value_two",
})
cl.Info("hello world")
}
Output is:
INFO[0000] hello world extra_field_one="extra_value_one" extra_field_two="extra_value_two"