Browse Source

Add an example hook which adds default fields

tags/v1.4.0
David Bariod 10 months ago
parent
commit
a99ca4776d
2 changed files with 31 additions and 1 deletions
  1. 30
    0
      example_default_field_value_test.go
  2. 1
    1
      example_global_hook_test.go

+ 30
- 0
example_default_field_value_test.go View File

@@ -0,0 +1,30 @@
1
+package logrus_test
2
+
3
+import (
4
+	"github.com/sirupsen/logrus"
5
+	"os"
6
+)
7
+
8
+type DefaultFieldHook struct {
9
+	GetValue func() string
10
+}
11
+
12
+func (h *DefaultFieldHook) Levels() []logrus.Level {
13
+	return logrus.AllLevels
14
+}
15
+
16
+func (h *DefaultFieldHook) Fire(e *logrus.Entry) error {
17
+	e.Data["aDefaultField"] = h.GetValue()
18
+	return nil
19
+}
20
+
21
+func ExampleDefaultField() {
22
+	l := logrus.New()
23
+	l.Out = os.Stdout
24
+	l.Formatter = &logrus.TextFormatter{DisableTimestamp: true, DisableColors: true}
25
+
26
+	l.AddHook(&DefaultFieldHook{GetValue: func() string { return "with its default value" }})
27
+	l.Info("first log")
28
+	// Output:
29
+	// level=info msg="first log" aDefaultField="with its default value"
30
+}

+ 1
- 1
example_global_hook_test.go View File

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

Loading…
Cancel
Save