Skip to content

Commit

Permalink
Merge pull request sirupsen#889 from sirupsen/default_field_hook
Browse files Browse the repository at this point in the history
Add an example hook which adds default fields
  • Loading branch information
dgsb committed Jan 17, 2019
2 parents 6df18b1 + b622c3a commit 87c44ef
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 30 additions & 0 deletions example_default_field_value_test.go
Original file line number Diff line number Diff line change
@@ -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"
}
2 changes: 1 addition & 1 deletion example_global_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 87c44ef

Please sign in to comment.