format.go 7.8 KB

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