MCPcopy
hub / github.com/uber-go/zap / EncodeEntry

Method EncodeEntry

zapcore/console_encoder.go:70–130  ·  view source on GitHub ↗
(ent Entry, fields []Field)

Source from the content-addressed store, hash-verified

68}
69
70func (c consoleEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer, error) {
71 line := bufferpool.Get()
72
73 // We don't want the entry's metadata to be quoted and escaped (if it's
74 // encoded as strings), which means that we can't use the JSON encoder. The
75 // simplest option is to use the memory encoder and fmt.Fprint.
76 //
77 // If this ever becomes a performance bottleneck, we can implement
78 // ArrayEncoder for our plain-text format.
79 arr := getSliceEncoder()
80 if c.TimeKey != "" && c.EncodeTime != nil && !ent.Time.IsZero() {
81 c.EncodeTime(ent.Time, arr)
82 }
83 if c.LevelKey != "" && c.EncodeLevel != nil {
84 c.EncodeLevel(ent.Level, arr)
85 }
86 if ent.LoggerName != "" && c.NameKey != "" {
87 nameEncoder := c.EncodeName
88
89 if nameEncoder == nil {
90 // Fall back to FullNameEncoder for backward compatibility.
91 nameEncoder = FullNameEncoder
92 }
93
94 nameEncoder(ent.LoggerName, arr)
95 }
96 if ent.Caller.Defined {
97 if c.CallerKey != "" && c.EncodeCaller != nil {
98 c.EncodeCaller(ent.Caller, arr)
99 }
100 if c.FunctionKey != "" {
101 arr.AppendString(ent.Caller.Function)
102 }
103 }
104 for i := range arr.elems {
105 if i > 0 {
106 line.AppendString(c.ConsoleSeparator)
107 }
108 _, _ = fmt.Fprint(line, arr.elems[i])
109 }
110 putSliceEncoder(arr)
111
112 // Add the message itself.
113 if c.MessageKey != "" {
114 c.addSeparatorIfNecessary(line)
115 line.AppendString(ent.Message)
116 }
117
118 // Add any structured context.
119 c.writeContext(line, fields)
120
121 // If there's no stacktrace key, honor that; this allows users to force
122 // single-line output.
123 if ent.Stack != "" && c.StacktraceKey != "" {
124 line.AppendByte('\n')
125 line.AppendString(ent.Stack)
126 }
127

Callers

nothing calls this directly

Calls 7

writeContextMethod · 0.95
getSliceEncoderFunction · 0.85
putSliceEncoderFunction · 0.85
AppendByteMethod · 0.80
AppendStringMethod · 0.65
GetMethod · 0.45

Tested by

no test coverage detected