forked from sirupsen/logrus
-
Notifications
You must be signed in to change notification settings - Fork 0
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 sirupsen#842 from dgsb/master
Add an example for tracing global variable with hook
- Loading branch information
Showing
3 changed files
with
39 additions
and
0 deletions.
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,36 @@ | ||
package logrus_test | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
"os" | ||
) | ||
|
||
var ( | ||
mystring string | ||
) | ||
|
||
type GlobalHook struct { | ||
} | ||
|
||
func (h *GlobalHook) Levels() []logrus.Level { | ||
return logrus.AllLevels | ||
} | ||
|
||
func (h *GlobalHook) Fire(e *logrus.Entry) error { | ||
e.Data["mystring"] = mystring | ||
return nil | ||
} | ||
|
||
func Example() { | ||
l := logrus.New() | ||
l.Out = os.Stdout | ||
l.Formatter = &logrus.TextFormatter{DisableTimestamp: true, DisableColors: true} | ||
l.AddHook(&GlobalHook{}) | ||
mystring = "first value" | ||
l.Info("first log") | ||
mystring = "another value" | ||
l.Info("second log") | ||
// Output: | ||
// level=info msg="first log" mystring="first value" | ||
// level=info msg="second log" mystring="another 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
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