stdlib.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package log
  2. import (
  3. "io"
  4. "log"
  5. "regexp"
  6. "strings"
  7. )
  8. // StdlibWriter implements io.Writer by invoking the stdlib log.Print. It's
  9. // designed to be passed to a Go kit logger as the writer, for cases where
  10. // it's necessary to redirect all Go kit log output to the stdlib logger.
  11. //
  12. // If you have any choice in the matter, you shouldn't use this. Prefer to
  13. // redirect the stdlib log to the Go kit logger via NewStdlibAdapter.
  14. type StdlibWriter struct{}
  15. // Write implements io.Writer.
  16. func (w StdlibWriter) Write(p []byte) (int, error) {
  17. log.Print(strings.TrimSpace(string(p)))
  18. return len(p), nil
  19. }
  20. // StdlibAdapter wraps a Logger and allows it to be passed to the stdlib
  21. // logger's SetOutput. It will extract date/timestamps, filenames, and
  22. // messages, and place them under relevant keys.
  23. type StdlibAdapter struct {
  24. Logger
  25. timestampKey string
  26. fileKey string
  27. messageKey string
  28. }
  29. // StdlibAdapterOption sets a parameter for the StdlibAdapter.
  30. type StdlibAdapterOption func(*StdlibAdapter)
  31. // TimestampKey sets the key for the timestamp field. By default, it's "ts".
  32. func TimestampKey(key string) StdlibAdapterOption {
  33. return func(a *StdlibAdapter) { a.timestampKey = key }
  34. }
  35. // FileKey sets the key for the file and line field. By default, it's "caller".
  36. func FileKey(key string) StdlibAdapterOption {
  37. return func(a *StdlibAdapter) { a.fileKey = key }
  38. }
  39. // MessageKey sets the key for the actual log message. By default, it's "msg".
  40. func MessageKey(key string) StdlibAdapterOption {
  41. return func(a *StdlibAdapter) { a.messageKey = key }
  42. }
  43. // NewStdlibAdapter returns a new StdlibAdapter wrapper around the passed
  44. // logger. It's designed to be passed to log.SetOutput.
  45. func NewStdlibAdapter(logger Logger, options ...StdlibAdapterOption) io.Writer {
  46. a := StdlibAdapter{
  47. Logger: logger,
  48. timestampKey: "ts",
  49. fileKey: "caller",
  50. messageKey: "msg",
  51. }
  52. for _, option := range options {
  53. option(&a)
  54. }
  55. return a
  56. }
  57. func (a StdlibAdapter) Write(p []byte) (int, error) {
  58. result := subexps(p)
  59. keyvals := []interface{}{}
  60. var timestamp string
  61. if date, ok := result["date"]; ok && date != "" {
  62. timestamp = date
  63. }
  64. if time, ok := result["time"]; ok && time != "" {
  65. if timestamp != "" {
  66. timestamp += " "
  67. }
  68. timestamp += time
  69. }
  70. if timestamp != "" {
  71. keyvals = append(keyvals, a.timestampKey, timestamp)
  72. }
  73. if file, ok := result["file"]; ok && file != "" {
  74. keyvals = append(keyvals, a.fileKey, file)
  75. }
  76. if msg, ok := result["msg"]; ok {
  77. keyvals = append(keyvals, a.messageKey, msg)
  78. }
  79. if err := a.Logger.Log(keyvals...); err != nil {
  80. return 0, err
  81. }
  82. return len(p), nil
  83. }
  84. const (
  85. logRegexpDate = `(?P<date>[0-9]{4}/[0-9]{2}/[0-9]{2})?[ ]?`
  86. logRegexpTime = `(?P<time>[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?)?[ ]?`
  87. logRegexpFile = `(?P<file>.+?:[0-9]+)?`
  88. logRegexpMsg = `(: )?(?P<msg>.*)`
  89. )
  90. var (
  91. logRegexp = regexp.MustCompile(logRegexpDate + logRegexpTime + logRegexpFile + logRegexpMsg)
  92. )
  93. func subexps(line []byte) map[string]string {
  94. m := logRegexp.FindSubmatch(line)
  95. if len(m) < len(logRegexp.SubexpNames()) {
  96. return map[string]string{}
  97. }
  98. result := map[string]string{}
  99. for i, name := range logRegexp.SubexpNames() {
  100. result[name] = string(m[i])
  101. }
  102. return result
  103. }