pattern.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package log
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "path"
  7. "runtime"
  8. "strings"
  9. "sync"
  10. "time"
  11. )
  12. var patternMap = map[string]func(map[string]interface{}) string{
  13. "T": longTime,
  14. "t": shortTime,
  15. "D": longDate,
  16. "d": shortDate,
  17. "L": keyFactory(_level),
  18. "f": keyFactory(_source),
  19. "i": keyFactory(_instanceID),
  20. "e": keyFactory(_deplyEnv),
  21. "z": keyFactory(_zone),
  22. "S": longSource,
  23. "s": shortSource,
  24. "M": message,
  25. }
  26. // newPatternRender new pattern render
  27. func newPatternRender(format string) Render {
  28. p := &pattern{
  29. bufPool: sync.Pool{New: func() interface{} { return &bytes.Buffer{} }},
  30. }
  31. b := make([]byte, 0, len(format))
  32. for i := 0; i < len(format); i++ {
  33. if format[i] != '%' {
  34. b = append(b, format[i])
  35. continue
  36. }
  37. if i+1 >= len(format) {
  38. b = append(b, format[i])
  39. continue
  40. }
  41. f, ok := patternMap[string(format[i+1])]
  42. if !ok {
  43. b = append(b, format[i])
  44. continue
  45. }
  46. if len(b) != 0 {
  47. p.funcs = append(p.funcs, textFactory(string(b)))
  48. b = b[:0]
  49. }
  50. p.funcs = append(p.funcs, f)
  51. i++
  52. }
  53. if len(b) != 0 {
  54. p.funcs = append(p.funcs, textFactory(string(b)))
  55. }
  56. return p
  57. }
  58. type pattern struct {
  59. funcs []func(map[string]interface{}) string
  60. bufPool sync.Pool
  61. }
  62. // Render implemet Formater
  63. func (p *pattern) Render(w io.Writer, d map[string]interface{}) error {
  64. buf := p.bufPool.Get().(*bytes.Buffer)
  65. defer func() {
  66. buf.Reset()
  67. p.bufPool.Put(buf)
  68. }()
  69. for _, f := range p.funcs {
  70. buf.WriteString(f(d))
  71. }
  72. _, err := buf.WriteTo(w)
  73. return err
  74. }
  75. // Render implemet Formater as string
  76. func (p *pattern) RenderString(d map[string]interface{}) string {
  77. // TODO strings.Builder
  78. buf := p.bufPool.Get().(*bytes.Buffer)
  79. defer func() {
  80. buf.Reset()
  81. p.bufPool.Put(buf)
  82. }()
  83. for _, f := range p.funcs {
  84. buf.WriteString(f(d))
  85. }
  86. return buf.String()
  87. }
  88. func textFactory(text string) func(map[string]interface{}) string {
  89. return func(map[string]interface{}) string {
  90. return text
  91. }
  92. }
  93. func keyFactory(key string) func(map[string]interface{}) string {
  94. return func(d map[string]interface{}) string {
  95. if v, ok := d[key]; ok {
  96. if s, ok := v.(string); ok {
  97. return s
  98. }
  99. return fmt.Sprint(v)
  100. }
  101. return ""
  102. }
  103. }
  104. func longSource(map[string]interface{}) string {
  105. if _, file, lineNo, ok := runtime.Caller(6); ok {
  106. return fmt.Sprintf("%s:%d", file, lineNo)
  107. }
  108. return "unknown:0"
  109. }
  110. func shortSource(map[string]interface{}) string {
  111. if _, file, lineNo, ok := runtime.Caller(6); ok {
  112. return fmt.Sprintf("%s:%d", path.Base(file), lineNo)
  113. }
  114. return "unknown:0"
  115. }
  116. func longTime(map[string]interface{}) string {
  117. return time.Now().Format("15:04:05.000")
  118. }
  119. func shortTime(map[string]interface{}) string {
  120. return time.Now().Format("15:04")
  121. }
  122. func longDate(map[string]interface{}) string {
  123. return time.Now().Format("2006/01/02")
  124. }
  125. func shortDate(map[string]interface{}) string {
  126. return time.Now().Format("01/02")
  127. }
  128. func isInternalKey(k string) bool {
  129. switch k {
  130. case _level, _levelValue, _time, _source, _instanceID, _appID, _deplyEnv, _zone:
  131. return true
  132. }
  133. return false
  134. }
  135. func message(d map[string]interface{}) string {
  136. var m string
  137. var s []string
  138. for k, v := range d {
  139. if k == _log {
  140. m = fmt.Sprint(v)
  141. continue
  142. }
  143. if isInternalKey(k) {
  144. continue
  145. }
  146. s = append(s, fmt.Sprintf("%s=%v", k, v))
  147. }
  148. s = append(s, m)
  149. return strings.Join(s, " ")
  150. }