-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #889 from sirupsen/default_field_hook
Add an example hook which adds default fields
- Loading branch information
Showing
2 changed files
with
31 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters