logging.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package logging
  2. import (
  3. "fmt"
  4. "log"
  5. golf "github.com/aphistic/golf"
  6. )
  7. /*
  8. ServiceLogger main type for logging
  9. */
  10. type ServiceLogger struct {
  11. GelfURL string
  12. GelfPort int
  13. SystemID string
  14. Attrs map[string]interface{}
  15. gelfActive bool
  16. c *golf.Client
  17. }
  18. /*
  19. InitGelf initialise gelf logging
  20. */
  21. func (s *ServiceLogger) InitGelf() {
  22. s.gelfActive = false
  23. if s.GelfURL != "" {
  24. s.c, _ = golf.NewClient()
  25. s.c.Dial(fmt.Sprintf("udp://%s:%d", s.GelfURL, s.GelfPort))
  26. l, _ := s.c.NewLogger()
  27. golf.DefaultLogger(l)
  28. for key, value := range s.Attrs {
  29. l.SetAttr(key, value)
  30. }
  31. l.SetAttr("system_id", s.SystemID)
  32. s.gelfActive = true
  33. }
  34. }
  35. /*
  36. Debug log this maeesage at debug level
  37. */
  38. func (s *ServiceLogger) Debug(msg string) {
  39. if s.gelfActive {
  40. golf.Info(msg)
  41. }
  42. log.Println(msg)
  43. }
  44. /*
  45. Debugf log this maeesage at debug level with formatting
  46. */
  47. func (s *ServiceLogger) Debugf(format string, va ...interface{}) {
  48. if s.gelfActive {
  49. golf.Infof(format, va...)
  50. }
  51. log.Printf(format+"\n", va...)
  52. }
  53. /*
  54. Info log this maeesage at info level
  55. */
  56. func (s *ServiceLogger) Info(msg string) {
  57. if s.gelfActive {
  58. golf.Info(msg)
  59. }
  60. log.Println(msg)
  61. }
  62. /*
  63. Infof log this maeesage at info level with formatting
  64. */
  65. func (s *ServiceLogger) Infof(format string, va ...interface{}) {
  66. if s.gelfActive {
  67. golf.Infof(format, va...)
  68. }
  69. log.Printf(format+"\n", va...)
  70. }
  71. /*
  72. Alert log this maeesage at alert level
  73. */
  74. func (s *ServiceLogger) Alert(msg string) {
  75. if s.gelfActive {
  76. golf.Alert(msg)
  77. }
  78. log.Printf("Alert: %s\n", msg)
  79. }
  80. /*
  81. Alertf log this maeesage at alert level with formatting
  82. */
  83. func (s *ServiceLogger) Alertf(format string, va ...interface{}) {
  84. if s.gelfActive {
  85. golf.Alertf(format, va...)
  86. }
  87. log.Printf("Alert: %s\n", fmt.Sprintf(format, va...))
  88. }
  89. // Fatal logs a message at level Fatal on the standard logger.
  90. func (s *ServiceLogger) Fatal(msg string) {
  91. if s.gelfActive {
  92. golf.Crit(msg)
  93. }
  94. log.Printf("Fatal: %s\n", msg)
  95. }
  96. // Fatalf logs a message at level Fatal on the standard logger.
  97. func (s *ServiceLogger) Fatalf(format string, va ...interface{}) {
  98. if s.gelfActive {
  99. golf.Critf(format, va...)
  100. }
  101. log.Printf("Fatal: %s\n", fmt.Sprintf(format, va...))
  102. }
  103. /*
  104. Close this logging client
  105. */
  106. func (s *ServiceLogger) Close() {
  107. if s.gelfActive {
  108. s.c.Close()
  109. }
  110. }