...

Source file src/github.com/sirupsen/logrus/example_global_hook_test.go

Documentation: github.com/sirupsen/logrus

     1  package logrus_test
     2  
     3  import (
     4  	"github.com/sirupsen/logrus"
     5  	"os"
     6  )
     7  
     8  var (
     9  	mystring string
    10  )
    11  
    12  type GlobalHook struct {
    13  }
    14  
    15  func (h *GlobalHook) Levels() []logrus.Level {
    16  	return logrus.AllLevels
    17  }
    18  
    19  func (h *GlobalHook) Fire(e *logrus.Entry) error {
    20  	e.Data["mystring"] = mystring
    21  	return nil
    22  }
    23  
    24  func Example() {
    25  	l := logrus.New()
    26  	l.Out = os.Stdout
    27  	l.Formatter = &logrus.TextFormatter{DisableTimestamp: true, DisableColors: true}
    28  	l.AddHook(&GlobalHook{})
    29  	mystring = "first value"
    30  	l.Info("first log")
    31  	mystring = "another value"
    32  	l.Info("second log")
    33  	// Output:
    34  	// level=info msg="first log" mystring="first value"
    35  	// level=info msg="second log" mystring="another value"
    36  }
    37  

View as plain text