syslog_formatter.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2015 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // +build !windows,!nacl,!plan9
  14. package log
  15. import (
  16. "fmt"
  17. "log/syslog"
  18. "os"
  19. "github.com/sirupsen/logrus"
  20. )
  21. var _ logrus.Formatter = (*syslogger)(nil)
  22. func init() {
  23. setSyslogFormatter = func(l logger, appname, local string) error {
  24. if appname == "" {
  25. return fmt.Errorf("missing appname parameter")
  26. }
  27. if local == "" {
  28. return fmt.Errorf("missing local parameter")
  29. }
  30. fmter, err := newSyslogger(appname, local, l.entry.Logger.Formatter)
  31. if err != nil {
  32. fmt.Fprintf(os.Stderr, "error creating syslog formatter: %v\n", err)
  33. l.entry.Errorf("can't connect logger to syslog: %v", err)
  34. return err
  35. }
  36. l.entry.Logger.Formatter = fmter
  37. return nil
  38. }
  39. }
  40. var prefixTag []byte
  41. type syslogger struct {
  42. wrap logrus.Formatter
  43. out *syslog.Writer
  44. }
  45. func newSyslogger(appname string, facility string, fmter logrus.Formatter) (*syslogger, error) {
  46. priority, err := getFacility(facility)
  47. if err != nil {
  48. return nil, err
  49. }
  50. out, err := syslog.New(priority, appname)
  51. _, isJSON := fmter.(*logrus.JSONFormatter)
  52. if isJSON {
  53. // add cee tag to json formatted syslogs
  54. prefixTag = []byte("@cee:")
  55. }
  56. return &syslogger{
  57. out: out,
  58. wrap: fmter,
  59. }, err
  60. }
  61. func getFacility(facility string) (syslog.Priority, error) {
  62. switch facility {
  63. case "0":
  64. return syslog.LOG_LOCAL0, nil
  65. case "1":
  66. return syslog.LOG_LOCAL1, nil
  67. case "2":
  68. return syslog.LOG_LOCAL2, nil
  69. case "3":
  70. return syslog.LOG_LOCAL3, nil
  71. case "4":
  72. return syslog.LOG_LOCAL4, nil
  73. case "5":
  74. return syslog.LOG_LOCAL5, nil
  75. case "6":
  76. return syslog.LOG_LOCAL6, nil
  77. case "7":
  78. return syslog.LOG_LOCAL7, nil
  79. }
  80. return syslog.LOG_LOCAL0, fmt.Errorf("invalid local(%s) for syslog", facility)
  81. }
  82. func (s *syslogger) Format(e *logrus.Entry) ([]byte, error) {
  83. data, err := s.wrap.Format(e)
  84. if err != nil {
  85. fmt.Fprintf(os.Stderr, "syslogger: can't format entry: %v\n", err)
  86. return data, err
  87. }
  88. // only append tag to data sent to syslog (line), not to what
  89. // is returned
  90. line := string(append(prefixTag, data...))
  91. switch e.Level {
  92. case logrus.PanicLevel:
  93. err = s.out.Crit(line)
  94. case logrus.FatalLevel:
  95. err = s.out.Crit(line)
  96. case logrus.ErrorLevel:
  97. err = s.out.Err(line)
  98. case logrus.WarnLevel:
  99. err = s.out.Warning(line)
  100. case logrus.InfoLevel:
  101. err = s.out.Info(line)
  102. case logrus.DebugLevel:
  103. err = s.out.Debug(line)
  104. default:
  105. err = s.out.Notice(line)
  106. }
  107. if err != nil {
  108. fmt.Fprintf(os.Stderr, "syslogger: can't send log to syslog: %v\n", err)
  109. }
  110. return data, err
  111. }