You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.2 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. package log
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "sync"
  7. "time"
  8. kitlog "github.com/go-kit/kit/log"
  9. kitlevel "github.com/go-kit/kit/log/level"
  10. "github.com/go-logfmt/logfmt"
  11. )
  12. type tmfmtEncoder struct {
  13. *logfmt.Encoder
  14. buf bytes.Buffer
  15. }
  16. func (l *tmfmtEncoder) Reset() {
  17. l.Encoder.Reset()
  18. l.buf.Reset()
  19. }
  20. var tmfmtEncoderPool = sync.Pool{
  21. New: func() interface{} {
  22. var enc tmfmtEncoder
  23. enc.Encoder = logfmt.NewEncoder(&enc.buf)
  24. return &enc
  25. },
  26. }
  27. type tmfmtLogger struct {
  28. w io.Writer
  29. }
  30. // NewTMFmtLogger returns a logger that encodes keyvals to the Writer in
  31. // Tendermint custom format. Note complex types (structs, maps, slices)
  32. // formatted as "%+v".
  33. //
  34. // Each log event produces no more than one call to w.Write.
  35. // The passed Writer must be safe for concurrent use by multiple goroutines if
  36. // the returned Logger will be used concurrently.
  37. func NewTMFmtLogger(w io.Writer) kitlog.Logger {
  38. return &tmfmtLogger{w}
  39. }
  40. func (l tmfmtLogger) Log(keyvals ...interface{}) error {
  41. enc := tmfmtEncoderPool.Get().(*tmfmtEncoder)
  42. enc.Reset()
  43. defer tmfmtEncoderPool.Put(enc)
  44. const unknown = "unknown"
  45. lvl := "none"
  46. msg := unknown
  47. module := unknown
  48. // indexes of keys to skip while encoding later
  49. excludeIndexes := make([]int, 0)
  50. for i := 0; i < len(keyvals)-1; i += 2 {
  51. // Extract level
  52. switch keyvals[i] {
  53. case kitlevel.Key():
  54. excludeIndexes = append(excludeIndexes, i)
  55. switch keyvals[i+1].(type) { // nolint:gocritic
  56. case string:
  57. lvl = keyvals[i+1].(string)
  58. case kitlevel.Value:
  59. lvl = keyvals[i+1].(kitlevel.Value).String()
  60. default:
  61. panic(fmt.Sprintf("level value of unknown type %T", keyvals[i+1]))
  62. }
  63. // and message
  64. case msgKey:
  65. excludeIndexes = append(excludeIndexes, i)
  66. msg = keyvals[i+1].(string)
  67. // and module (could be multiple keyvals; if such case last keyvalue wins)
  68. case moduleKey:
  69. excludeIndexes = append(excludeIndexes, i)
  70. module = keyvals[i+1].(string)
  71. }
  72. }
  73. // Form a custom Tendermint line
  74. //
  75. // Example:
  76. // D[2016-05-02|11:06:44.322] Stopping AddrBook (ignoring: already stopped)
  77. //
  78. // Description:
  79. // D - first character of the level, uppercase (ASCII only)
  80. // [2016-05-02|11:06:44.322] - our time format (see https://golang.org/src/time/format.go)
  81. // Stopping ... - message
  82. enc.buf.WriteString(fmt.Sprintf("%c[%s] %-44s ", lvl[0]-32, time.Now().Format("2006-01-02|15:04:05.000"), msg))
  83. if module != unknown {
  84. enc.buf.WriteString("module=" + module + " ")
  85. }
  86. KeyvalueLoop:
  87. for i := 0; i < len(keyvals)-1; i += 2 {
  88. for _, j := range excludeIndexes {
  89. if i == j {
  90. continue KeyvalueLoop
  91. }
  92. }
  93. err := enc.EncodeKeyval(keyvals[i], keyvals[i+1])
  94. if err == logfmt.ErrUnsupportedValueType {
  95. enc.EncodeKeyval(keyvals[i], fmt.Sprintf("%+v", keyvals[i+1]))
  96. } else if err != nil {
  97. return err
  98. }
  99. }
  100. // Add newline to the end of the buffer
  101. if err := enc.EndRecord(); err != nil {
  102. return err
  103. }
  104. // The Logger interface requires implementations to be safe for concurrent
  105. // use by multiple goroutines. For this implementation that means making
  106. // only one call to l.w.Write() for each call to Log.
  107. if _, err := l.w.Write(enc.buf.Bytes()); err != nil {
  108. return err
  109. }
  110. return nil
  111. }