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.

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