1 // Copyright (c) 2016 Uber Technologies, Inc.
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:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
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
27 "go.uber.org/zap/buffer"
28 "go.uber.org/zap/internal/bufferpool"
31 var _sliceEncoderPool = sync.Pool{
32 New: func() interface{} {
33 return &sliceArrayEncoder{elems: make([]interface{}, 0, 2)}
37 func getSliceEncoder() *sliceArrayEncoder {
38 return _sliceEncoderPool.Get().(*sliceArrayEncoder)
41 func putSliceEncoder(e *sliceArrayEncoder) {
43 _sliceEncoderPool.Put(e)
46 type consoleEncoder struct {
50 // NewConsoleEncoder creates an encoder whose output is designed for human -
51 // rather than machine - consumption. It serializes the core log entry data
52 // (message, level, timestamp, etc.) in a plain-text format and leaves the
53 // structured context as JSON.
55 // Note that although the console encoder doesn't use the keys specified in the
56 // encoder configuration, it will omit any element whose key is set to the empty
58 func NewConsoleEncoder(cfg EncoderConfig) Encoder {
59 return consoleEncoder{newJSONEncoder(cfg, true)}
62 func (c consoleEncoder) Clone() Encoder {
63 return consoleEncoder{c.jsonEncoder.Clone().(*jsonEncoder)}
66 func (c consoleEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer, error) {
67 line := bufferpool.Get()
69 // We don't want the entry's metadata to be quoted and escaped (if it's
70 // encoded as strings), which means that we can't use the JSON encoder. The
71 // simplest option is to use the memory encoder and fmt.Fprint.
73 // If this ever becomes a performance bottleneck, we can implement
74 // ArrayEncoder for our plain-text format.
75 arr := getSliceEncoder()
76 if c.TimeKey != "" && c.EncodeTime != nil {
77 c.EncodeTime(ent.Time, arr)
79 if c.LevelKey != "" && c.EncodeLevel != nil {
80 c.EncodeLevel(ent.Level, arr)
82 if ent.LoggerName != "" && c.NameKey != "" {
83 nameEncoder := c.EncodeName
85 if nameEncoder == nil {
86 // Fall back to FullNameEncoder for backward compatibility.
87 nameEncoder = FullNameEncoder
90 nameEncoder(ent.LoggerName, arr)
92 if ent.Caller.Defined && c.CallerKey != "" && c.EncodeCaller != nil {
93 c.EncodeCaller(ent.Caller, arr)
95 for i := range arr.elems {
99 fmt.Fprint(line, arr.elems[i])
103 // Add the message itself.
104 if c.MessageKey != "" {
105 c.addTabIfNecessary(line)
106 line.AppendString(ent.Message)
109 // Add any structured context.
110 c.writeContext(line, fields)
112 // If there's no stacktrace key, honor that; this allows users to force
113 // single-line output.
114 if ent.Stack != "" && c.StacktraceKey != "" {
115 line.AppendByte('\n')
116 line.AppendString(ent.Stack)
119 if c.LineEnding != "" {
120 line.AppendString(c.LineEnding)
122 line.AppendString(DefaultLineEnding)
127 func (c consoleEncoder) writeContext(line *buffer.Buffer, extra []Field) {
128 context := c.jsonEncoder.Clone().(*jsonEncoder)
129 defer context.buf.Free()
131 addFields(context, extra)
132 context.closeOpenNamespaces()
133 if context.buf.Len() == 0 {
137 c.addTabIfNecessary(line)
139 line.Write(context.buf.Bytes())
143 func (c consoleEncoder) addTabIfNecessary(line *buffer.Buffer) {
145 line.AppendByte('\t')