format.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package formatter
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "text/template"
  7. "time"
  8. "gopkg.in/mcuadros/go-syslog.v2/format"
  9. "gopkg.in/mgo.v2/bson"
  10. )
  11. // Formatter is an interface, so that multiple implementations can exist
  12. type Formatter func(format.LogParts) string
  13. var tmplFuncs template.FuncMap
  14. var syslogTmpl *template.Template
  15. func init() {
  16. tmplFuncs := template.FuncMap{
  17. "dateFormat": func(dt time.Time, fmt string) string {
  18. return dt.Format(fmt)
  19. },
  20. "rfc822": func(dt time.Time) string {
  21. return dt.Format(time.RFC822)
  22. },
  23. "sevName": func(s int) string {
  24. names := []string{"emerg", "alert", "crit", "err", "warn", "notice", "info", "dbg"}
  25. if s >= len(names) {
  26. return "???"
  27. }
  28. return names[s]
  29. },
  30. }
  31. syslogTmpl = template.Must(template.New("syslog").Funcs(tmplFuncs).Parse(
  32. "{{rfc822 (index . \"timestamp\")}} {{index . \"hostname\"}} " +
  33. "{{index . \"app_name\"}}" +
  34. "{{ if (ne (index . \"proc_id\") \"-\")}}[{{index . \"proc_id\"}}]{{end}}: " +
  35. "<{{ sevName (index . \"severity\") }}> " +
  36. "{{index . \"message\"}}",
  37. ))
  38. }
  39. type Format int
  40. func (rf *Format) Set(v string) error {
  41. newvalue, err := parseFormat(v)
  42. if err != nil {
  43. return err
  44. }
  45. *rf = newvalue
  46. return nil
  47. }
  48. func (rf Format) String() string {
  49. switch rf {
  50. case FormatJSON:
  51. return "json"
  52. case FormatSyslog:
  53. return "syslog"
  54. case FormatBSON:
  55. return "bson"
  56. }
  57. return ""
  58. }
  59. func (rf Format) WriteFormatted(w io.Writer, msg format.LogParts) error {
  60. return WriteFormatted(w, rf, msg)
  61. }
  62. func parseFormat(s string) (Format, error) {
  63. switch s {
  64. case "json":
  65. return FormatJSON, nil
  66. case "syslog":
  67. return FormatSyslog, nil
  68. case "bson":
  69. return FormatBSON, nil
  70. default:
  71. return 0, fmt.Errorf("Undefined format `%s`", s)
  72. }
  73. }
  74. const (
  75. FormatSyslog = iota // 0
  76. FormatJSON = iota
  77. FormatBSON = iota
  78. )
  79. func WriteFormatted(w io.Writer, f Format, msg format.LogParts) error {
  80. switch f {
  81. case FormatSyslog:
  82. return syslogTmpl.Execute(w, msg)
  83. case FormatJSON:
  84. enc := json.NewEncoder(w)
  85. enc.SetIndent("", " ")
  86. return enc.Encode(msg)
  87. case FormatBSON:
  88. enc, err := bson.Marshal(msg)
  89. if err != nil {
  90. return err
  91. }
  92. _, err = w.Write(enc)
  93. return err
  94. }
  95. return nil
  96. }