formatter.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package srslog
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. )
  7. // Formatter is a type of function that takes the consituent parts of a
  8. // syslog message and returns a formatted string. A different Formatter is
  9. // defined for each different syslog protocol we support.
  10. type Formatter func(p Priority, hostname, tag, content string) string
  11. // DefaultFormatter is the original format supported by the Go syslog package,
  12. // and is a non-compliant amalgamation of 3164 and 5424 that is intended to
  13. // maximize compatibility.
  14. func DefaultFormatter(p Priority, hostname, tag, content string) string {
  15. timestamp := time.Now().Format(time.RFC3339)
  16. msg := fmt.Sprintf("<%d> %s %s %s[%d]: %s",
  17. p, timestamp, hostname, tag, os.Getpid(), content)
  18. return msg
  19. }
  20. // UnixFormatter omits the hostname, because it is only used locally.
  21. func UnixFormatter(p Priority, hostname, tag, content string) string {
  22. timestamp := time.Now().Format(time.Stamp)
  23. msg := fmt.Sprintf("<%d>%s %s[%d]: %s",
  24. p, timestamp, tag, os.Getpid(), content)
  25. return msg
  26. }
  27. // RFC3164Formatter provides an RFC 3164 compliant message.
  28. func RFC3164Formatter(p Priority, hostname, tag, content string) string {
  29. timestamp := time.Now().Format(time.Stamp)
  30. msg := fmt.Sprintf("<%d> %s %s %s[%d]: %s",
  31. p, timestamp, hostname, tag, os.Getpid(), content)
  32. return msg
  33. }
  34. // RFC5424Formatter provides an RFC 5424 compliant message.
  35. func RFC5424Formatter(p Priority, hostname, tag, content string) string {
  36. timestamp := time.Now().Format(time.RFC3339)
  37. pid := os.Getpid()
  38. appName := os.Args[0]
  39. msg := fmt.Sprintf("<%d>%d %s %s %s %d %s %s",
  40. p, 1, timestamp, hostname, appName, pid, tag, content)
  41. return msg
  42. }