format field content style
This commit is contained in:
parent
ac33bdc69f
commit
6ac40ca1a2
@ -55,7 +55,7 @@ func NewTextEncoder(cfg zapcore.EncoderConfig) zapcore.Encoder {
|
|||||||
return &textEncoder{
|
return &textEncoder{
|
||||||
EncoderConfig: &cfg,
|
EncoderConfig: &cfg,
|
||||||
buf: bufferpool.Get(),
|
buf: bufferpool.Get(),
|
||||||
spaced: false,
|
spaced: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,12 +68,13 @@ func (enc textEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*
|
|||||||
if enc.TimeKey != "" && enc.EncodeTime != nil {
|
if enc.TimeKey != "" && enc.EncodeTime != nil {
|
||||||
enc.EncodeTime(ent.Time, arr)
|
enc.EncodeTime(ent.Time, arr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if enc.LevelKey != "" && enc.EncodeLevel != nil {
|
if enc.LevelKey != "" && enc.EncodeLevel != nil {
|
||||||
enc.EncodeLevel(ent.Level, arr)
|
enc.EncodeLevel(ent.Level, arr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ent.LoggerName != "" && enc.NameKey != "" {
|
if ent.LoggerName != "" && enc.NameKey != "" {
|
||||||
nameEncoder := enc.EncodeName
|
nameEncoder := enc.EncodeName
|
||||||
|
|
||||||
if nameEncoder == nil {
|
if nameEncoder == nil {
|
||||||
// Fall back to FullNameEncoder for backward compatibility.
|
// Fall back to FullNameEncoder for backward compatibility.
|
||||||
nameEncoder = zapcore.FullNameEncoder
|
nameEncoder = zapcore.FullNameEncoder
|
||||||
@ -81,17 +82,23 @@ func (enc textEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*
|
|||||||
|
|
||||||
nameEncoder(ent.LoggerName, arr)
|
nameEncoder(ent.LoggerName, arr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ent.Caller.Defined && enc.CallerKey != "" && enc.EncodeCaller != nil {
|
if ent.Caller.Defined && enc.CallerKey != "" && enc.EncodeCaller != nil {
|
||||||
enc.EncodeCaller(ent.Caller, arr)
|
enc.EncodeCaller(ent.Caller, arr)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range arr.elems {
|
for i := range arr.elems {
|
||||||
// if i > 0 {
|
// if i > 0 {
|
||||||
// line.AppendByte('\t')
|
// line.AppendByte('\t')
|
||||||
// }
|
// }
|
||||||
_, _ = fmt.Fprint(line, arr.elems[i])
|
_, _ = fmt.Fprint(line, arr.elems[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
putSliceEncoder(arr)
|
putSliceEncoder(arr)
|
||||||
|
|
||||||
|
// Add any structured context.
|
||||||
|
enc.writeContext(line, fields)
|
||||||
|
|
||||||
// Add the message itself.
|
// Add the message itself.
|
||||||
if enc.MessageKey != "" {
|
if enc.MessageKey != "" {
|
||||||
// c.addTabIfNecessary(line)
|
// c.addTabIfNecessary(line)
|
||||||
@ -99,9 +106,6 @@ func (enc textEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*
|
|||||||
line.AppendString(ent.Message)
|
line.AppendString(ent.Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add any structured context.
|
|
||||||
enc.writeContext(line, fields)
|
|
||||||
|
|
||||||
// If there's no stacktrace key, honor that; this allows users to force
|
// If there's no stacktrace key, honor that; this allows users to force
|
||||||
// single-line output.
|
// single-line output.
|
||||||
if ent.Stack != "" && enc.StacktraceKey != "" {
|
if ent.Stack != "" && enc.StacktraceKey != "" {
|
||||||
@ -114,6 +118,7 @@ func (enc textEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*
|
|||||||
} else {
|
} else {
|
||||||
line.AppendString(zapcore.DefaultLineEnding)
|
line.AppendString(zapcore.DefaultLineEnding)
|
||||||
}
|
}
|
||||||
|
|
||||||
return line, nil
|
return line, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,9 +133,10 @@ func (enc textEncoder) writeContext(line *buffer.Buffer, extra []zapcore.Field)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// c.addTabIfNecessary(line)
|
// c.addTabIfNecessary(line)
|
||||||
line.AppendByte('{')
|
// line.AppendByte('{')
|
||||||
|
line.AppendByte(' ')
|
||||||
_, _ = line.Write(context.buf.Bytes())
|
_, _ = line.Write(context.buf.Bytes())
|
||||||
line.AppendByte('}')
|
// line.AppendByte('}')
|
||||||
}
|
}
|
||||||
|
|
||||||
func (enc textEncoder) addTabIfNecessary(line *buffer.Buffer) {
|
func (enc textEncoder) addTabIfNecessary(line *buffer.Buffer) {
|
||||||
@ -241,9 +247,10 @@ func (enc *textEncoder) OpenNamespace(key string) {
|
|||||||
func (enc *textEncoder) AddString(key, val string) {
|
func (enc *textEncoder) AddString(key, val string) {
|
||||||
enc.addKey(key)
|
enc.addKey(key)
|
||||||
enc.addElementSeparator()
|
enc.addElementSeparator()
|
||||||
enc.buf.AppendByte('"')
|
// enc.buf.AppendByte('"')
|
||||||
enc.safeAddString(val)
|
enc.safeAddString(val)
|
||||||
enc.buf.AppendByte('"')
|
// enc.buf.AppendByte('"')
|
||||||
|
enc.buf.AppendByte(']')
|
||||||
}
|
}
|
||||||
func (enc *textEncoder) AddTime(key string, val time.Time) {
|
func (enc *textEncoder) AddTime(key string, val time.Time) {
|
||||||
enc.addKey(key)
|
enc.addKey(key)
|
||||||
@ -293,9 +300,10 @@ func (enc *textEncoder) clone() *textEncoder {
|
|||||||
|
|
||||||
func (enc *textEncoder) addKey(key string) {
|
func (enc *textEncoder) addKey(key string) {
|
||||||
enc.addElementSeparator()
|
enc.addElementSeparator()
|
||||||
enc.buf.AppendByte('"')
|
// enc.buf.AppendByte('"')
|
||||||
|
enc.buf.AppendByte('[')
|
||||||
enc.safeAddString(key)
|
enc.safeAddString(key)
|
||||||
enc.buf.AppendByte('"')
|
// enc.buf.AppendByte('"')
|
||||||
enc.buf.AppendByte(':')
|
enc.buf.AppendByte(':')
|
||||||
if enc.spaced {
|
if enc.spaced {
|
||||||
enc.buf.AppendByte(' ')
|
enc.buf.AppendByte(' ')
|
||||||
|
@ -15,13 +15,13 @@ func TestLog(t *testing.T) {
|
|||||||
|
|
||||||
func TestLogWithConfig(t *testing.T) {
|
func TestLogWithConfig(t *testing.T) {
|
||||||
config := NewLogConfig()
|
config := NewLogConfig()
|
||||||
_ = config.Level.Set("d")
|
_ = config.Level.Set("debug")
|
||||||
config.Name = "main"
|
config.Name = "main"
|
||||||
// config.Fields = []zap.Field{zap.String("traceid", "12123123123")}
|
// config.Fields = []zap.Field{zap.String("traceid", "12123123123")}
|
||||||
|
|
||||||
config.Init()
|
config.Init()
|
||||||
|
|
||||||
With("foo", "baz")
|
With("traceid", "21221212122")
|
||||||
Debugf("this is %s message", "debug")
|
Debugf("this is %s message", "debug")
|
||||||
config.Init()
|
config.Init()
|
||||||
With(zap.String("traceid", "12123123123"))
|
With(zap.String("traceid", "12123123123"))
|
||||||
|
Loading…
Reference in New Issue
Block a user