You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.0 KiB

  1. package logrus_test
  2. import (
  3. "github.com/sirupsen/logrus"
  4. "os"
  5. )
  6. func Example_basic() {
  7. var log = logrus.New()
  8. log.Formatter = new(logrus.JSONFormatter)
  9. log.Formatter = new(logrus.TextFormatter) //default
  10. log.Formatter.(*logrus.TextFormatter).DisableTimestamp = true // remove timestamp from test output
  11. log.Level = logrus.DebugLevel
  12. log.Out = os.Stdout
  13. // file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666)
  14. // if err == nil {
  15. // log.Out = file
  16. // } else {
  17. // log.Info("Failed to log to file, using default stderr")
  18. // }
  19. defer func() {
  20. err := recover()
  21. if err != nil {
  22. entry := err.(*logrus.Entry)
  23. log.WithFields(logrus.Fields{
  24. "omg": true,
  25. "err_animal": entry.Data["animal"],
  26. "err_size": entry.Data["size"],
  27. "err_level": entry.Level,
  28. "err_message": entry.Message,
  29. "number": 100,
  30. }).Error("The ice breaks!") // or use Fatal() to force the process to exit with a nonzero code
  31. }
  32. }()
  33. log.WithFields(logrus.Fields{
  34. "animal": "walrus",
  35. "number": 8,
  36. }).Debug("Started observing beach")
  37. log.WithFields(logrus.Fields{
  38. "animal": "walrus",
  39. "size": 10,
  40. }).Info("A group of walrus emerges from the ocean")
  41. log.WithFields(logrus.Fields{
  42. "omg": true,
  43. "number": 122,
  44. }).Warn("The group's number increased tremendously!")
  45. log.WithFields(logrus.Fields{
  46. "temperature": -4,
  47. }).Debug("Temperature changes")
  48. log.WithFields(logrus.Fields{
  49. "animal": "orca",
  50. "size": 9009,
  51. }).Panic("It's over 9000!")
  52. // Output:
  53. // level=debug msg="Started observing beach" animal=walrus number=8
  54. // level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10
  55. // level=warning msg="The group's number increased tremendously!" number=122 omg=true
  56. // level=debug msg="Temperature changes" temperature=-4
  57. // level=panic msg="It's over 9000!" animal=orca size=9009
  58. // level=error msg="The ice breaks!" err_animal=orca err_level=panic err_message="It's over 9000!" err_size=9009 number=100 omg=true
  59. }