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.

116 lines
2.6 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. lvlIndex := -1
  46. msgIndex := -1
  47. for i := 0; i < len(keyvals)-1; i += 2 {
  48. // Extract level
  49. if keyvals[i] == kitlevel.Key() {
  50. lvlIndex = i
  51. switch keyvals[i+1].(type) {
  52. case string:
  53. lvl = keyvals[i+1].(string)
  54. case kitlevel.Value:
  55. lvl = keyvals[i+1].(kitlevel.Value).String()
  56. default:
  57. panic(fmt.Sprintf("level value of unknown type %T", keyvals[i+1]))
  58. }
  59. continue
  60. }
  61. // and message
  62. if keyvals[i] == msgKey {
  63. msgIndex = i
  64. msg = keyvals[i+1].(string)
  65. continue
  66. }
  67. if lvlIndex > 0 && msgIndex > 0 { // found all we're looking for
  68. break
  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. for i := 0; i < len(keyvals)-1; i += 2 {
  82. if i == lvlIndex || i == msgIndex {
  83. continue
  84. }
  85. if err := enc.EncodeKeyval(keyvals[i], keyvals[i+1]); err != nil {
  86. return err
  87. }
  88. }
  89. // Add newline to the end of the buffer
  90. if err := enc.EndRecord(); err != nil {
  91. return err
  92. }
  93. // The Logger interface requires implementations to be safe for concurrent
  94. // use by multiple goroutines. For this implementation that means making
  95. // only one call to l.w.Write() for each call to Log.
  96. if _, err := l.w.Write(enc.buf.Bytes()); err != nil {
  97. return err
  98. }
  99. return nil
  100. }