format.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package log
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "reflect"
  7. "strconv"
  8. "strings"
  9. "sync"
  10. "sync/atomic"
  11. "time"
  12. "unicode/utf8"
  13. )
  14. const (
  15. timeFormat = "2006-01-02T15:04:05-0700"
  16. termTimeFormat = "01-02|15:04:05"
  17. floatFormat = 'f'
  18. termMsgJust = 40
  19. )
  20. // locationTrims are trimmed for display to avoid unwieldy log lines.
  21. var locationTrims = []string{
  22. "github.com/ethereum/go-ethereum/",
  23. }
  24. // PrintOrigins sets or unsets log location (file:line) printing for terminal
  25. // format output.
  26. func PrintOrigins(print bool) {
  27. if print {
  28. atomic.StoreUint32(&locationEnabled, 1)
  29. } else {
  30. atomic.StoreUint32(&locationEnabled, 0)
  31. }
  32. }
  33. // locationEnabled is an atomic flag controlling whether the terminal formatter
  34. // should append the log locations too when printing entries.
  35. var locationEnabled uint32
  36. // locationLength is the maxmimum path length encountered, which all logs are
  37. // padded to to aid in alignment.
  38. var locationLength uint32
  39. // fieldPadding is a global map with maximum field value lengths seen until now
  40. // to allow padding log contexts in a bit smarter way.
  41. var fieldPadding = make(map[string]int)
  42. // fieldPaddingLock is a global mutex protecting the field padding map.
  43. var fieldPaddingLock sync.RWMutex
  44. type Format interface {
  45. Format(r *Record) []byte
  46. }
  47. // FormatFunc returns a new Format object which uses
  48. // the given function to perform record formatting.
  49. func FormatFunc(f func(*Record) []byte) Format {
  50. return formatFunc(f)
  51. }
  52. type formatFunc func(*Record) []byte
  53. func (f formatFunc) Format(r *Record) []byte {
  54. return f(r)
  55. }
  56. // TerminalStringer is an analogous interface to the stdlib stringer, allowing
  57. // own types to have custom shortened serialization formats when printed to the
  58. // screen.
  59. type TerminalStringer interface {
  60. TerminalString() string
  61. }
  62. // TerminalFormat formats log records optimized for human readability on
  63. // a terminal with color-coded level output and terser human friendly timestamp.
  64. // This format should only be used for interactive programs or while developing.
  65. //
  66. // [TIME] [LEVEL] MESAGE key=value key=value ...
  67. //
  68. // Example:
  69. //
  70. // [May 16 20:58:45] [DBUG] remove route ns=haproxy addr=127.0.0.1:50002
  71. //
  72. func TerminalFormat(usecolor bool) Format {
  73. return FormatFunc(func(r *Record) []byte {
  74. var color = 0
  75. if usecolor {
  76. switch r.Lvl {
  77. case LvlCrit:
  78. color = 35
  79. case LvlError:
  80. color = 31
  81. case LvlWarn:
  82. color = 33
  83. case LvlInfo:
  84. color = 32
  85. case LvlDebug:
  86. color = 36
  87. case LvlTrace:
  88. color = 34
  89. }
  90. }
  91. b := &bytes.Buffer{}
  92. lvl := r.Lvl.AlignedString()
  93. if atomic.LoadUint32(&locationEnabled) != 0 {
  94. // Log origin printing was requested, format the location path and line number
  95. location := fmt.Sprintf("%+v", r.Call)
  96. for _, prefix := range locationTrims {
  97. location = strings.TrimPrefix(location, prefix)
  98. }
  99. // Maintain the maximum location length for fancyer alignment
  100. align := int(atomic.LoadUint32(&locationLength))
  101. if align < len(location) {
  102. align = len(location)
  103. atomic.StoreUint32(&locationLength, uint32(align))
  104. }
  105. padding := strings.Repeat(" ", align-len(location))
  106. // Assemble and print the log heading
  107. if color > 0 {
  108. fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s|%s]%s %s ", color, lvl, r.Time.Format(termTimeFormat), location, padding, r.Msg)
  109. } else {
  110. fmt.Fprintf(b, "%s[%s|%s]%s %s ", lvl, r.Time.Format(termTimeFormat), location, padding, r.Msg)
  111. }
  112. } else {
  113. if color > 0 {
  114. fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %s ", color, lvl, r.Time.Format(termTimeFormat), r.Msg)
  115. } else {
  116. fmt.Fprintf(b, "%s[%s] %s ", lvl, r.Time.Format(termTimeFormat), r.Msg)
  117. }
  118. }
  119. // try to justify the log output for short messages
  120. length := utf8.RuneCountInString(r.Msg)
  121. if len(r.Ctx) > 0 && length < termMsgJust {
  122. b.Write(bytes.Repeat([]byte{' '}, termMsgJust-length))
  123. }
  124. // print the keys logfmt style
  125. logfmt(b, r.Ctx, color, true)
  126. return b.Bytes()
  127. })
  128. }
  129. // LogfmtFormat prints records in logfmt format, an easy machine-parseable but human-readable
  130. // format for key/value pairs.
  131. //
  132. // For more details see: http://godoc.org/github.com/kr/logfmt
  133. //
  134. func LogfmtFormat() Format {
  135. return FormatFunc(func(r *Record) []byte {
  136. common := []interface{}{r.KeyNames.Time, r.Time, r.KeyNames.Lvl, r.Lvl, r.KeyNames.Msg, r.Msg}
  137. buf := &bytes.Buffer{}
  138. logfmt(buf, append(common, r.Ctx...), 0, false)
  139. return buf.Bytes()
  140. })
  141. }
  142. func logfmt(buf *bytes.Buffer, ctx []interface{}, color int, term bool) {
  143. for i := 0; i < len(ctx); i += 2 {
  144. if i != 0 {
  145. buf.WriteByte(' ')
  146. }
  147. k, ok := ctx[i].(string)
  148. v := formatLogfmtValue(ctx[i+1], term)
  149. if !ok {
  150. k, v = errorKey, formatLogfmtValue(k, term)
  151. }
  152. // XXX: we should probably check that all of your key bytes aren't invalid
  153. fieldPaddingLock.RLock()
  154. padding := fieldPadding[k]
  155. fieldPaddingLock.RUnlock()
  156. length := utf8.RuneCountInString(v)
  157. if padding < length {
  158. padding = length
  159. fieldPaddingLock.Lock()
  160. fieldPadding[k] = padding
  161. fieldPaddingLock.Unlock()
  162. }
  163. if color > 0 {
  164. fmt.Fprintf(buf, "\x1b[%dm%s\x1b[0m=", color, k)
  165. } else {
  166. buf.WriteString(k)
  167. buf.WriteByte('=')
  168. }
  169. buf.WriteString(v)
  170. if i < len(ctx)-2 {
  171. buf.Write(bytes.Repeat([]byte{' '}, padding-length))
  172. }
  173. }
  174. buf.WriteByte('\n')
  175. }
  176. // JSONFormat formats log records as JSON objects separated by newlines.
  177. // It is the equivalent of JSONFormatEx(false, true).
  178. func JSONFormat() Format {
  179. return JSONFormatEx(false, true)
  180. }
  181. // JSONFormatEx formats log records as JSON objects. If pretty is true,
  182. // records will be pretty-printed. If lineSeparated is true, records
  183. // will be logged with a new line between each record.
  184. func JSONFormatEx(pretty, lineSeparated bool) Format {
  185. jsonMarshal := json.Marshal
  186. if pretty {
  187. jsonMarshal = func(v interface{}) ([]byte, error) {
  188. return json.MarshalIndent(v, "", " ")
  189. }
  190. }
  191. return FormatFunc(func(r *Record) []byte {
  192. props := make(map[string]interface{})
  193. props[r.KeyNames.Time] = r.Time
  194. props[r.KeyNames.Lvl] = r.Lvl.String()
  195. props[r.KeyNames.Msg] = r.Msg
  196. for i := 0; i < len(r.Ctx); i += 2 {
  197. k, ok := r.Ctx[i].(string)
  198. if !ok {
  199. props[errorKey] = fmt.Sprintf("%+v is not a string key", r.Ctx[i])
  200. }
  201. props[k] = formatJSONValue(r.Ctx[i+1])
  202. }
  203. b, err := jsonMarshal(props)
  204. if err != nil {
  205. b, _ = jsonMarshal(map[string]string{
  206. errorKey: err.Error(),
  207. })
  208. return b
  209. }
  210. if lineSeparated {
  211. b = append(b, '\n')
  212. }
  213. return b
  214. })
  215. }
  216. func formatShared(value interface{}) (result interface{}) {
  217. defer func() {
  218. if err := recover(); err != nil {
  219. if v := reflect.ValueOf(value); v.Kind() == reflect.Ptr && v.IsNil() {
  220. result = "nil"
  221. } else {
  222. panic(err)
  223. }
  224. }
  225. }()
  226. switch v := value.(type) {
  227. case time.Time:
  228. return v.Format(timeFormat)
  229. case error:
  230. return v.Error()
  231. case fmt.Stringer:
  232. return v.String()
  233. default:
  234. return v
  235. }
  236. }
  237. func formatJSONValue(value interface{}) interface{} {
  238. value = formatShared(value)
  239. switch value.(type) {
  240. case int, int8, int16, int32, int64, float32, float64, uint, uint8, uint16, uint32, uint64, string:
  241. return value
  242. default:
  243. return fmt.Sprintf("%+v", value)
  244. }
  245. }
  246. // formatValue formats a value for serialization
  247. func formatLogfmtValue(value interface{}, term bool) string {
  248. if value == nil {
  249. return "nil"
  250. }
  251. if t, ok := value.(time.Time); ok {
  252. // Performance optimization: No need for escaping since the provided
  253. // timeFormat doesn't have any escape characters, and escaping is
  254. // expensive.
  255. return t.Format(timeFormat)
  256. }
  257. if term {
  258. if s, ok := value.(TerminalStringer); ok {
  259. // Custom terminal stringer provided, use that
  260. return escapeString(s.TerminalString())
  261. }
  262. }
  263. value = formatShared(value)
  264. switch v := value.(type) {
  265. case bool:
  266. return strconv.FormatBool(v)
  267. case float32:
  268. return strconv.FormatFloat(float64(v), floatFormat, 3, 64)
  269. case float64:
  270. return strconv.FormatFloat(v, floatFormat, 3, 64)
  271. case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
  272. return fmt.Sprintf("%d", value)
  273. case string:
  274. return escapeString(v)
  275. default:
  276. return escapeString(fmt.Sprintf("%+v", value))
  277. }
  278. }
  279. var stringBufPool = sync.Pool{
  280. New: func() interface{} { return new(bytes.Buffer) },
  281. }
  282. func escapeString(s string) string {
  283. needsQuotes := false
  284. needsEscape := false
  285. for _, r := range s {
  286. if r <= ' ' || r == '=' || r == '"' {
  287. needsQuotes = true
  288. }
  289. if r == '\\' || r == '"' || r == '\n' || r == '\r' || r == '\t' {
  290. needsEscape = true
  291. }
  292. }
  293. if !needsEscape && !needsQuotes {
  294. return s
  295. }
  296. e := stringBufPool.Get().(*bytes.Buffer)
  297. e.WriteByte('"')
  298. for _, r := range s {
  299. switch r {
  300. case '\\', '"':
  301. e.WriteByte('\\')
  302. e.WriteByte(byte(r))
  303. case '\n':
  304. e.WriteString("\\n")
  305. case '\r':
  306. e.WriteString("\\r")
  307. case '\t':
  308. e.WriteString("\\t")
  309. default:
  310. e.WriteRune(r)
  311. }
  312. }
  313. e.WriteByte('"')
  314. var ret string
  315. if needsQuotes {
  316. ret = e.String()
  317. } else {
  318. ret = string(e.Bytes()[1 : e.Len()-1])
  319. }
  320. e.Reset()
  321. stringBufPool.Put(e)
  322. return ret
  323. }