diff --git a/example_default_field_value_test.go b/example_default_field_value_test.go new file mode 100644 index 0000000..a72ece4 --- /dev/null +++ b/example_default_field_value_test.go @@ -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" +} diff --git a/example_global_hook_test.go b/example_global_hook_test.go index c81e448..aaf2f4b 100644 --- a/example_global_hook_test.go +++ b/example_global_hook_test.go @@ -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}