format.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package log
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "reflect"
  7. "strconv"
  8. "strings"
  9. "sync"
  10. "time"
  11. )
  12. const (
  13. timeFormat = "2006-01-02T15:04:05-0700"
  14. termTimeFormat = "01-02|15:04:05"
  15. floatFormat = 'f'
  16. termMsgJust = 40
  17. )
  18. type Format interface {
  19. Format(r *Record) []byte
  20. }
  21. // FormatFunc returns a new Format object which uses
  22. // the given function to perform record formatting.
  23. func FormatFunc(f func(*Record) []byte) Format {
  24. return formatFunc(f)
  25. }
  26. type formatFunc func(*Record) []byte
  27. func (f formatFunc) Format(r *Record) []byte {
  28. return f(r)
  29. }
  30. // TerminalFormat formats log records optimized for human readability on
  31. // a terminal with color-coded level output and terser human friendly timestamp.
  32. // This format should only be used for interactive programs or while developing.
  33. //
  34. // [TIME] [LEVEL] MESAGE key=value key=value ...
  35. //
  36. // Example:
  37. //
  38. // [May 16 20:58:45] [DBUG] remove route ns=haproxy addr=127.0.0.1:50002
  39. //
  40. func TerminalFormat() Format {
  41. return FormatFunc(func(r *Record) []byte {
  42. var color = 0
  43. switch r.Lvl {
  44. case LvlCrit:
  45. color = 35
  46. case LvlError:
  47. color = 31
  48. case LvlWarn:
  49. color = 33
  50. case LvlInfo:
  51. color = 32
  52. case LvlDebug:
  53. color = 36
  54. case LvlTrace:
  55. color = 34
  56. }
  57. b := &bytes.Buffer{}
  58. lvl := strings.ToUpper(r.Lvl.String())
  59. if color > 0 {
  60. fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %s ", color, lvl, r.Time.Format(termTimeFormat), r.Msg)
  61. } else {
  62. fmt.Fprintf(b, "[%s] [%s] %s ", lvl, r.Time.Format(termTimeFormat), r.Msg)
  63. }
  64. // try to justify the log output for short messages
  65. if len(r.Ctx) > 0 && len(r.Msg) < termMsgJust {
  66. b.Write(bytes.Repeat([]byte{' '}, termMsgJust-len(r.Msg)))
  67. }
  68. // print the keys logfmt style
  69. logfmt(b, r.Ctx, color)
  70. return b.Bytes()
  71. })
  72. }
  73. // LogfmtFormat prints records in logfmt format, an easy machine-parseable but human-readable
  74. // format for key/value pairs.
  75. //
  76. // For more details see: http://godoc.org/github.com/kr/logfmt
  77. //
  78. func LogfmtFormat() Format {
  79. return FormatFunc(func(r *Record) []byte {
  80. common := []interface{}{r.KeyNames.Time, r.Time, r.KeyNames.Lvl, r.Lvl, r.KeyNames.Msg, r.Msg}
  81. buf := &bytes.Buffer{}
  82. logfmt(buf, append(common, r.Ctx...), 0)
  83. return buf.Bytes()
  84. })
  85. }
  86. func logfmt(buf *bytes.Buffer, ctx []interface{}, color int) {
  87. for i := 0; i < len(ctx); i += 2 {
  88. if i != 0 {
  89. buf.WriteByte(' ')
  90. }
  91. k, ok := ctx[i].(string)
  92. v := formatLogfmtValue(ctx[i+1])
  93. if !ok {
  94. k, v = errorKey, formatLogfmtValue(k)
  95. }
  96. // XXX: we should probably check that all of your key bytes aren't invalid
  97. if color > 0 {
  98. fmt.Fprintf(buf, "\x1b[%dm%s\x1b[0m=%s", color, k, v)
  99. } else {
  100. buf.WriteString(k)
  101. buf.WriteByte('=')
  102. buf.WriteString(v)
  103. }
  104. }
  105. buf.WriteByte('\n')
  106. }
  107. // JsonFormat formats log records as JSON objects separated by newlines.
  108. // It is the equivalent of JsonFormatEx(false, true).
  109. func JsonFormat() Format {
  110. return JsonFormatEx(false, true)
  111. }
  112. // JsonFormatEx formats log records as JSON objects. If pretty is true,
  113. // records will be pretty-printed. If lineSeparated is true, records
  114. // will be logged with a new line between each record.
  115. func JsonFormatEx(pretty, lineSeparated bool) Format {
  116. jsonMarshal := json.Marshal
  117. if pretty {
  118. jsonMarshal = func(v interface{}) ([]byte, error) {
  119. return json.MarshalIndent(v, "", " ")
  120. }
  121. }
  122. return FormatFunc(func(r *Record) []byte {
  123. props := make(map[string]interface{})
  124. props[r.KeyNames.Time] = r.Time
  125. props[r.KeyNames.Lvl] = r.Lvl.String()
  126. props[r.KeyNames.Msg] = r.Msg
  127. for i := 0; i < len(r.Ctx); i += 2 {
  128. k, ok := r.Ctx[i].(string)
  129. if !ok {
  130. props[errorKey] = fmt.Sprintf("%+v is not a string key", r.Ctx[i])
  131. }
  132. props[k] = formatJsonValue(r.Ctx[i+1])
  133. }
  134. b, err := jsonMarshal(props)
  135. if err != nil {
  136. b, _ = jsonMarshal(map[string]string{
  137. errorKey: err.Error(),
  138. })
  139. return b
  140. }
  141. if lineSeparated {
  142. b = append(b, '\n')
  143. }
  144. return b
  145. })
  146. }
  147. func formatShared(value interface{}) (result interface{}) {
  148. defer func() {
  149. if err := recover(); err != nil {
  150. if v := reflect.ValueOf(value); v.Kind() == reflect.Ptr && v.IsNil() {
  151. result = "nil"
  152. } else {
  153. panic(err)
  154. }
  155. }
  156. }()
  157. switch v := value.(type) {
  158. case time.Time:
  159. return v.Format(timeFormat)
  160. case error:
  161. return v.Error()
  162. case fmt.Stringer:
  163. return v.String()
  164. default:
  165. return v
  166. }
  167. }
  168. func formatJsonValue(value interface{}) interface{} {
  169. value = formatShared(value)
  170. switch value.(type) {
  171. case int, int8, int16, int32, int64, float32, float64, uint, uint8, uint16, uint32, uint64, string:
  172. return value
  173. default:
  174. return fmt.Sprintf("%+v", value)
  175. }
  176. }
  177. // formatValue formats a value for serialization
  178. func formatLogfmtValue(value interface{}) string {
  179. if value == nil {
  180. return "nil"
  181. }
  182. if t, ok := value.(time.Time); ok {
  183. // Performance optimization: No need for escaping since the provided
  184. // timeFormat doesn't have any escape characters, and escaping is
  185. // expensive.
  186. return t.Format(timeFormat)
  187. }
  188. value = formatShared(value)
  189. switch v := value.(type) {
  190. case bool:
  191. return strconv.FormatBool(v)
  192. case float32:
  193. return strconv.FormatFloat(float64(v), floatFormat, 3, 64)
  194. case float64:
  195. return strconv.FormatFloat(v, floatFormat, 3, 64)
  196. case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
  197. return fmt.Sprintf("%d", value)
  198. case string:
  199. return escapeString(v)
  200. default:
  201. return escapeString(fmt.Sprintf("%+v", value))
  202. }
  203. }
  204. var stringBufPool = sync.Pool{
  205. New: func() interface{} { return new(bytes.Buffer) },
  206. }
  207. func escapeString(s string) string {
  208. needsQuotes := false
  209. needsEscape := false
  210. for _, r := range s {
  211. if r <= ' ' || r == '=' || r == '"' {
  212. needsQuotes = true
  213. }
  214. if r == '\\' || r == '"' || r == '\n' || r == '\r' || r == '\t' {
  215. needsEscape = true
  216. }
  217. }
  218. if needsEscape == false && needsQuotes == false {
  219. return s
  220. }
  221. e := stringBufPool.Get().(*bytes.Buffer)
  222. e.WriteByte('"')
  223. for _, r := range s {
  224. switch r {
  225. case '\\', '"':
  226. e.WriteByte('\\')
  227. e.WriteByte(byte(r))
  228. case '\n':
  229. e.WriteString("\\n")
  230. case '\r':
  231. e.WriteString("\\r")
  232. case '\t':
  233. e.WriteString("\\t")
  234. default:
  235. e.WriteRune(r)
  236. }
  237. }
  238. e.WriteByte('"')
  239. var ret string
  240. if needsQuotes {
  241. ret = e.String()
  242. } else {
  243. ret = string(e.Bytes()[1 : e.Len()-1])
  244. }
  245. e.Reset()
  246. stringBufPool.Put(e)
  247. return ret
  248. }