From f2ab87f230e2c652a48c789c24add4e17dce84e9 Mon Sep 17 00:00:00 2001 From: David Bariod Date: Mon, 15 Oct 2018 21:20:03 +0200 Subject: [PATCH 1/2] Add an example for tracing global variable with hook --- example_global_hook_test.go | 37 +++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ 3 files changed, 40 insertions(+) create mode 100644 example_global_hook_test.go diff --git a/example_global_hook_test.go b/example_global_hook_test.go new file mode 100644 index 0000000..df1584c --- /dev/null +++ b/example_global_hook_test.go @@ -0,0 +1,37 @@ +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} + l.Formatter.(*logrus.TextFormatter).DisableTimestamp = 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" +} diff --git a/go.mod b/go.mod index f4fed02..79f11b7 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/konsorten/go-windows-terminal-sequences v0.0.0-20180402223658-b729f2633dfe github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.1.1 // indirect github.com/stretchr/testify v1.2.2 golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 diff --git a/go.sum b/go.sum index 1f0d719..16cab75 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/konsorten/go-windows-terminal-sequences v0.0.0-20180402223658-b729f26 github.com/konsorten/go-windows-terminal-sequences v0.0.0-20180402223658-b729f2633dfe/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I= From 9c7692ccff2dca67e6db9b6eaef21a2c37ad82e7 Mon Sep 17 00:00:00 2001 From: David Bariod Date: Mon, 15 Oct 2018 21:32:20 +0200 Subject: [PATCH 2/2] disable colors on hook example --- example_global_hook_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/example_global_hook_test.go b/example_global_hook_test.go index df1584c..c81e448 100644 --- a/example_global_hook_test.go +++ b/example_global_hook_test.go @@ -24,8 +24,7 @@ func (h *GlobalHook) Fire(e *logrus.Entry) error { func Example() { l := logrus.New() l.Out = os.Stdout - l.Formatter = &logrus.TextFormatter{DisableTimestamp: true} - l.Formatter.(*logrus.TextFormatter).DisableTimestamp = true + l.Formatter = &logrus.TextFormatter{DisableTimestamp: true, DisableColors: true} l.AddHook(&GlobalHook{}) mystring = "first value" l.Info("first log")