package zapcore
import (
"fmt"
"go.uber.org/zap/buffer"
"go.uber.org/zap/internal/bufferpool"
"go.uber.org/zap/internal/pool"
)
var _sliceEncoderPool = pool .New (func () *sliceArrayEncoder {
return &sliceArrayEncoder {
elems : make ([]interface {}, 0 , 2 ),
}
})
func getSliceEncoder () *sliceArrayEncoder {
return _sliceEncoderPool .Get ()
}
func putSliceEncoder (e *sliceArrayEncoder ) {
e .elems = e .elems [:0 ]
_sliceEncoderPool .Put (e )
}
type consoleEncoder struct {
*jsonEncoder
}
func NewConsoleEncoder (cfg EncoderConfig ) Encoder {
if cfg .ConsoleSeparator == "" {
cfg .ConsoleSeparator = "\t"
}
return consoleEncoder {newJSONEncoder (cfg , true )}
}
func (c consoleEncoder ) Clone () Encoder {
return consoleEncoder {c .jsonEncoder .Clone ().(*jsonEncoder )}
}
func (c consoleEncoder ) EncodeEntry (ent Entry , fields []Field ) (*buffer .Buffer , error ) {
line := bufferpool .Get ()
arr := getSliceEncoder ()
if c .TimeKey != "" && c .EncodeTime != nil {
c .EncodeTime (ent .Time , arr )
}
if c .LevelKey != "" && c .EncodeLevel != nil {
c .EncodeLevel (ent .Level , arr )
}
if ent .LoggerName != "" && c .NameKey != "" {
nameEncoder := c .EncodeName
if nameEncoder == nil {
nameEncoder = FullNameEncoder
}
nameEncoder (ent .LoggerName , arr )
}
if ent .Caller .Defined {
if c .CallerKey != "" && c .EncodeCaller != nil {
c .EncodeCaller (ent .Caller , arr )
}
if c .FunctionKey != "" {
arr .AppendString (ent .Caller .Function )
}
}
for i := range arr .elems {
if i > 0 {
line .AppendString (c .ConsoleSeparator )
}
fmt .Fprint (line , arr .elems [i ])
}
putSliceEncoder (arr )
if c .MessageKey != "" {
c .addSeparatorIfNecessary (line )
line .AppendString (ent .Message )
}
c .writeContext (line , fields )
if ent .Stack != "" && c .StacktraceKey != "" {
line .AppendByte ('\n' )
line .AppendString (ent .Stack )
}
line .AppendString (c .LineEnding )
return line , nil
}
func (c consoleEncoder ) writeContext (line *buffer .Buffer , extra []Field ) {
context := c .jsonEncoder .Clone ().(*jsonEncoder )
defer func () {
context .buf .Free ()
putJSONEncoder (context )
}()
addFields (context , extra )
context .closeOpenNamespaces ()
if context .buf .Len () == 0 {
return
}
c .addSeparatorIfNecessary (line )
line .AppendByte ('{' )
line .Write (context .buf .Bytes ())
line .AppendByte ('}' )
}
func (c consoleEncoder ) addSeparatorIfNecessary (line *buffer .Buffer ) {
if line .Len () > 0 {
line .AppendString (c .ConsoleSeparator )
}
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .