rfc.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package formatter
  2. import (
  3. "errors"
  4. "log"
  5. syslog "gopkg.in/mcuadros/go-syslog.v2"
  6. )
  7. type SyslogRFC string
  8. // SetSyslogFormat does the job of setting the server parser to the provided RFC log format.
  9. func SetSyslogFormat(server *syslog.Server, format SyslogRFC) {
  10. switch {
  11. case format == Auto:
  12. server.SetFormat(syslog.Automatic)
  13. case format == RFC3164:
  14. server.SetFormat(syslog.RFC3164)
  15. case format == RFC5424:
  16. server.SetFormat(syslog.RFC5424)
  17. }
  18. log.Printf("Syslog format set to: %s\n", format)
  19. }
  20. func (rfc *SyslogRFC) Set(v string) error {
  21. newval, err := parseRFCValue(v)
  22. if err != nil {
  23. return err
  24. }
  25. *rfc = newval
  26. return nil
  27. }
  28. func (rfc *SyslogRFC) String() string {
  29. switch {
  30. case *rfc == Auto:
  31. return "auto"
  32. case *rfc == RFC3164:
  33. return "rfc3164"
  34. case *rfc == RFC5424:
  35. return "rfc5424"
  36. }
  37. return ""
  38. }
  39. func parseRFCValue(v string) (SyslogRFC, error) {
  40. switch {
  41. case v == "rfc3164":
  42. return RFC3164, nil
  43. case v == "rfc5424":
  44. return RFC5424, nil
  45. case v == "auto":
  46. return Auto, nil
  47. default:
  48. return "", ErrRFCNotSupported
  49. }
  50. }
  51. const (
  52. RFC3164 = "rfc3164"
  53. RFC5424 = "rfc5424"
  54. Auto = "auto"
  55. )
  56. // ErrRFCNotSupported is raised if the supplied rfc string is
  57. // not recognized.
  58. var ErrRFCNotSupported = errors.New("RFC not known")