example_global_hook_test.go 680 B

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