Add an example hook which adds default fields

This commit is contained in:
David Bariod 2019-01-17 09:37:03 +01:00
parent 78fb3852d9
commit a99ca4776d
2 changed files with 31 additions and 1 deletions

View File

@ -0,0 +1,30 @@
package logrus_test
import (
"github.com/sirupsen/logrus"
"os"
)
type DefaultFieldHook struct {
GetValue func() string
}
func (h *DefaultFieldHook) Levels() []logrus.Level {
return logrus.AllLevels
}
func (h *DefaultFieldHook) Fire(e *logrus.Entry) error {
e.Data["aDefaultField"] = h.GetValue()
return nil
}
func ExampleDefaultField() {
l := logrus.New()
l.Out = os.Stdout
l.Formatter = &logrus.TextFormatter{DisableTimestamp: true, DisableColors: true}
l.AddHook(&DefaultFieldHook{GetValue: func() string { return "with its default value" }})
l.Info("first log")
// Output:
// level=info msg="first log" aDefaultField="with its default value"
}

View File

@ -21,7 +21,7 @@ func (h *GlobalHook) Fire(e *logrus.Entry) error {
return nil
}
func Example() {
func ExampleGlobalVariableHook() {
l := logrus.New()
l.Out = os.Stdout
l.Formatter = &logrus.TextFormatter{DisableTimestamp: true, DisableColors: true}