2
0

stdlib.go 4.2 KB

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