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.

123 lines
3.0 KiB

8 years ago
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.
  32. //
  33. // Each log event produces no more than one call to w.Write.
  34. // The passed Writer must be safe for concurrent use by multiple goroutines if
  35. // the returned Logger will be used concurrently.
  36. func NewTMFmtLogger(w io.Writer) kitlog.Logger {
  37. return &tmfmtLogger{w}
  38. }
  39. func (l tmfmtLogger) Log(keyvals ...interface{}) error {
  40. enc := tmfmtEncoderPool.Get().(*tmfmtEncoder)
  41. enc.Reset()
  42. defer tmfmtEncoderPool.Put(enc)
  43. const unknown = "unknown"
  44. lvl := "none"
  45. msg := unknown
  46. module := unknown
  47. // indexes of keys to skip while encoding later
  48. excludeIndexes := make([]int, 0)
  49. for i := 0; i < len(keyvals)-1; i += 2 {
  50. // Extract level
  51. if keyvals[i] == kitlevel.Key() {
  52. excludeIndexes = append(excludeIndexes, i)
  53. switch keyvals[i+1].(type) {
  54. case string:
  55. lvl = keyvals[i+1].(string)
  56. case kitlevel.Value:
  57. lvl = keyvals[i+1].(kitlevel.Value).String()
  58. default:
  59. panic(fmt.Sprintf("level value of unknown type %T", keyvals[i+1]))
  60. }
  61. // and message
  62. } else if keyvals[i] == msgKey {
  63. excludeIndexes = append(excludeIndexes, i)
  64. msg = keyvals[i+1].(string)
  65. // and module (could be multiple keyvals; if such case last keyvalue wins)
  66. } else if keyvals[i] == moduleKey {
  67. excludeIndexes = append(excludeIndexes, i)
  68. module = keyvals[i+1].(string)
  69. }
  70. }
  71. // Form a custom Tendermint line
  72. //
  73. // Example:
  74. // D[05-02|11:06:44.322] Stopping AddrBook (ignoring: already stopped)
  75. //
  76. // Description:
  77. // D - first character of the level, uppercase (ASCII only)
  78. // [05-02|11:06:44.322] - our time format (see https://golang.org/src/time/format.go)
  79. // Stopping ... - message
  80. enc.buf.WriteString(fmt.Sprintf("%c[%s] %-44s ", lvl[0]-32, time.Now().UTC().Format("01-02|15:04:05.000"), msg))
  81. if module != unknown {
  82. enc.buf.WriteString("module=" + module + " ")
  83. }
  84. KeyvalueLoop:
  85. for i := 0; i < len(keyvals)-1; i += 2 {
  86. for _, j := range excludeIndexes {
  87. if i == j {
  88. continue KeyvalueLoop
  89. }
  90. }
  91. if err := enc.EncodeKeyval(keyvals[i], keyvals[i+1]); err != nil {
  92. return err
  93. }
  94. }
  95. // Add newline to the end of the buffer
  96. if err := enc.EndRecord(); err != nil {
  97. return err
  98. }
  99. // The Logger interface requires implementations to be safe for concurrent
  100. // use by multiple goroutines. For this implementation that means making
  101. // only one call to l.w.Write() for each call to Log.
  102. if _, err := l.w.Write(enc.buf.Bytes()); err != nil {
  103. return err
  104. }
  105. return nil
  106. }