format.go 10 KB

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