logrus/example_global_hook_test.go

38 lines
691 B
Go
Raw Permalink Normal View History

package logrus_test
import (
"os"
"github.com/sirupsen/logrus"
)
var (
mystring string
)
type GlobalHook struct {
}
func (h *GlobalHook) Levels() []logrus.Level {
return logrus.AllLevels
}
func (h *GlobalHook) Fire(e *logrus.Entry) error {
e.Data["mystring"] = mystring
return nil
}
func ExampleGlobalHook() {
l := logrus.New()
l.Out = os.Stdout
2018-10-15 22:32:20 +03:00
l.Formatter = &logrus.TextFormatter{DisableTimestamp: true, DisableColors: true}
l.AddHook(&GlobalHook{})
mystring = "first value"
l.Info("first log")
mystring = "another value"
l.Info("second log")
// Output:
// level=info msg="first log" mystring="first value"
// level=info msg="second log" mystring="another value"
}