console_encoder.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright (c) 2016 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package zapcore
  21. import (
  22. "fmt"
  23. "sync"
  24. "go.uber.org/zap/buffer"
  25. "go.uber.org/zap/internal/bufferpool"
  26. )
  27. var _sliceEncoderPool = sync.Pool{
  28. New: func() interface{} {
  29. return &sliceArrayEncoder{elems: make([]interface{}, 0, 2)}
  30. },
  31. }
  32. func getSliceEncoder() *sliceArrayEncoder {
  33. return _sliceEncoderPool.Get().(*sliceArrayEncoder)
  34. }
  35. func putSliceEncoder(e *sliceArrayEncoder) {
  36. e.elems = e.elems[:0]
  37. _sliceEncoderPool.Put(e)
  38. }
  39. type consoleEncoder struct {
  40. *jsonEncoder
  41. }
  42. // NewConsoleEncoder creates an encoder whose output is designed for human -
  43. // rather than machine - consumption. It serializes the core log entry data
  44. // (message, level, timestamp, etc.) in a plain-text format and leaves the
  45. // structured context as JSON.
  46. //
  47. // Note that although the console encoder doesn't use the keys specified in the
  48. // encoder configuration, it will omit any element whose key is set to the empty
  49. // string.
  50. func NewConsoleEncoder(cfg EncoderConfig) Encoder {
  51. return consoleEncoder{newJSONEncoder(cfg, true)}
  52. }
  53. func (c consoleEncoder) Clone() Encoder {
  54. return consoleEncoder{c.jsonEncoder.Clone().(*jsonEncoder)}
  55. }
  56. func (c consoleEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer, error) {
  57. line := bufferpool.Get()
  58. // We don't want the entry's metadata to be quoted and escaped (if it's
  59. // encoded as strings), which means that we can't use the JSON encoder. The
  60. // simplest option is to use the memory encoder and fmt.Fprint.
  61. //
  62. // If this ever becomes a performance bottleneck, we can implement
  63. // ArrayEncoder for our plain-text format.
  64. arr := getSliceEncoder()
  65. if c.TimeKey != "" && c.EncodeTime != nil {
  66. c.EncodeTime(ent.Time, arr)
  67. }
  68. if c.LevelKey != "" && c.EncodeLevel != nil {
  69. c.EncodeLevel(ent.Level, arr)
  70. }
  71. if ent.LoggerName != "" && c.NameKey != "" {
  72. nameEncoder := c.EncodeName
  73. if nameEncoder == nil {
  74. // Fall back to FullNameEncoder for backward compatibility.
  75. nameEncoder = FullNameEncoder
  76. }
  77. nameEncoder(ent.LoggerName, arr)
  78. }
  79. if ent.Caller.Defined && c.CallerKey != "" && c.EncodeCaller != nil {
  80. c.EncodeCaller(ent.Caller, arr)
  81. }
  82. for i := range arr.elems {
  83. if i > 0 {
  84. line.AppendByte('\t')
  85. }
  86. fmt.Fprint(line, arr.elems[i])
  87. }
  88. putSliceEncoder(arr)
  89. // Add the message itself.
  90. if c.MessageKey != "" {
  91. c.addTabIfNecessary(line)
  92. line.AppendString(ent.Message)
  93. }
  94. // Add any structured context.
  95. c.writeContext(line, fields)
  96. // If there's no stacktrace key, honor that; this allows users to force
  97. // single-line output.
  98. if ent.Stack != "" && c.StacktraceKey != "" {
  99. line.AppendByte('\n')
  100. line.AppendString(ent.Stack)
  101. }
  102. if c.LineEnding != "" {
  103. line.AppendString(c.LineEnding)
  104. } else {
  105. line.AppendString(DefaultLineEnding)
  106. }
  107. return line, nil
  108. }
  109. func (c consoleEncoder) writeContext(line *buffer.Buffer, extra []Field) {
  110. context := c.jsonEncoder.Clone().(*jsonEncoder)
  111. defer context.buf.Free()
  112. addFields(context, extra)
  113. context.closeOpenNamespaces()
  114. if context.buf.Len() == 0 {
  115. return
  116. }
  117. c.addTabIfNecessary(line)
  118. line.AppendByte('{')
  119. line.Write(context.buf.Bytes())
  120. line.AppendByte('}')
  121. }
  122. func (c consoleEncoder) addTabIfNecessary(line *buffer.Buffer) {
  123. if line.Len() > 0 {
  124. line.AppendByte('\t')
  125. }
  126. }