example_hook_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // +build !windows
  2. package logrus_test
  3. import (
  4. "log/syslog"
  5. "os"
  6. "github.com/sirupsen/logrus"
  7. slhooks "github.com/sirupsen/logrus/hooks/syslog"
  8. )
  9. // An example on how to use a hook
  10. func Example_hook() {
  11. var log = logrus.New()
  12. log.Formatter = new(logrus.TextFormatter) // default
  13. log.Formatter.(*logrus.TextFormatter).DisableColors = true // remove colors
  14. log.Formatter.(*logrus.TextFormatter).DisableTimestamp = true // remove timestamp from test output
  15. if sl, err := slhooks.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, ""); err != nil {
  16. log.Hooks.Add(sl)
  17. }
  18. log.Out = os.Stdout
  19. log.WithFields(logrus.Fields{
  20. "animal": "walrus",
  21. "size": 10,
  22. }).Info("A group of walrus emerges from the ocean")
  23. log.WithFields(logrus.Fields{
  24. "omg": true,
  25. "number": 122,
  26. }).Warn("The group's number increased tremendously!")
  27. log.WithFields(logrus.Fields{
  28. "omg": true,
  29. "number": 100,
  30. }).Error("The ice breaks!")
  31. // Output:
  32. // level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10
  33. // level=warning msg="The group's number increased tremendously!" number=122 omg=true
  34. // level=error msg="The ice breaks!" number=100 omg=true
  35. }