From a99ca4776daa02a26c3f57289df035d89fe7113f Mon Sep 17 00:00:00 2001 From: David Bariod Date: Thu, 17 Jan 2019 09:37:03 +0100 Subject: [PATCH] Add an example hook which adds default fields --- example_default_field_value_test.go | 30 +++++++++++++++++++++++++++++ example_global_hook_test.go | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 example_default_field_value_test.go 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}