Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / go.uber.org / zap / zapcore / console_encoder.go
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
21 package zapcore
22
23 import (
24         "fmt"
25         "sync"
26
27         "go.uber.org/zap/buffer"
28         "go.uber.org/zap/internal/bufferpool"
29 )
30
31 var _sliceEncoderPool = sync.Pool{
32         New: func() interface{} {
33                 return &sliceArrayEncoder{elems: make([]interface{}, 0, 2)}
34         },
35 }
36
37 func getSliceEncoder() *sliceArrayEncoder {
38         return _sliceEncoderPool.Get().(*sliceArrayEncoder)
39 }
40
41 func putSliceEncoder(e *sliceArrayEncoder) {
42         e.elems = e.elems[:0]
43         _sliceEncoderPool.Put(e)
44 }
45
46 type consoleEncoder struct {
47         *jsonEncoder
48 }
49
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.
54 //
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
57 // string.
58 func NewConsoleEncoder(cfg EncoderConfig) Encoder {
59         return consoleEncoder{newJSONEncoder(cfg, true)}
60 }
61
62 func (c consoleEncoder) Clone() Encoder {
63         return consoleEncoder{c.jsonEncoder.Clone().(*jsonEncoder)}
64 }
65
66 func (c consoleEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer, error) {
67         line := bufferpool.Get()
68
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.
72         //
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)
78         }
79         if c.LevelKey != "" && c.EncodeLevel != nil {
80                 c.EncodeLevel(ent.Level, arr)
81         }
82         if ent.LoggerName != "" && c.NameKey != "" {
83                 nameEncoder := c.EncodeName
84
85                 if nameEncoder == nil {
86                         // Fall back to FullNameEncoder for backward compatibility.
87                         nameEncoder = FullNameEncoder
88                 }
89
90                 nameEncoder(ent.LoggerName, arr)
91         }
92         if ent.Caller.Defined && c.CallerKey != "" && c.EncodeCaller != nil {
93                 c.EncodeCaller(ent.Caller, arr)
94         }
95         for i := range arr.elems {
96                 if i > 0 {
97                         line.AppendByte('\t')
98                 }
99                 fmt.Fprint(line, arr.elems[i])
100         }
101         putSliceEncoder(arr)
102
103         // Add the message itself.
104         if c.MessageKey != "" {
105                 c.addTabIfNecessary(line)
106                 line.AppendString(ent.Message)
107         }
108
109         // Add any structured context.
110         c.writeContext(line, fields)
111
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)
117         }
118
119         if c.LineEnding != "" {
120                 line.AppendString(c.LineEnding)
121         } else {
122                 line.AppendString(DefaultLineEnding)
123         }
124         return line, nil
125 }
126
127 func (c consoleEncoder) writeContext(line *buffer.Buffer, extra []Field) {
128         context := c.jsonEncoder.Clone().(*jsonEncoder)
129         defer context.buf.Free()
130
131         addFields(context, extra)
132         context.closeOpenNamespaces()
133         if context.buf.Len() == 0 {
134                 return
135         }
136
137         c.addTabIfNecessary(line)
138         line.AppendByte('{')
139         line.Write(context.buf.Bytes())
140         line.AppendByte('}')
141 }
142
143 func (c consoleEncoder) addTabIfNecessary(line *buffer.Buffer) {
144         if line.Len() > 0 {
145                 line.AppendByte('\t')
146         }
147 }