Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support configurable delimiter for console encoder #697

Merged
merged 14 commits into from Jul 17, 2020
Merged
16 changes: 9 additions & 7 deletions zapcore/console_encoder.go
Expand Up @@ -22,10 +22,9 @@ package zapcore

import (
"fmt"
"sync"

"go.uber.org/zap/buffer"
"go.uber.org/zap/internal/bufferpool"
"sync"
lixingwang marked this conversation as resolved.
Show resolved Hide resolved
lixingwang marked this conversation as resolved.
Show resolved Hide resolved
)

var _sliceEncoderPool = sync.Pool{
Expand Down Expand Up @@ -56,6 +55,9 @@ type consoleEncoder struct {
// encoder configuration, it will omit any element whose key is set to the empty
// string.
func NewConsoleEncoder(cfg EncoderConfig) Encoder {
if cfg.ElementDelimiter == 0 {
lixingwang marked this conversation as resolved.
Show resolved Hide resolved
cfg.ElementDelimiter = '\t'
}
return consoleEncoder{newJSONEncoder(cfg, true)}
}

Expand Down Expand Up @@ -94,15 +96,15 @@ func (c consoleEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer,
}
for i := range arr.elems {
if i > 0 {
line.AppendByte('\t')
line.AppendByte(c.ElementDelimiter)
}
fmt.Fprint(line, arr.elems[i])
}
putSliceEncoder(arr)

// Add the message itself.
if c.MessageKey != "" {
c.addTabIfNecessary(line)
c.addDelimiterIfNecessary(line)
line.AppendString(ent.Message)
}

Expand Down Expand Up @@ -134,14 +136,14 @@ func (c consoleEncoder) writeContext(line *buffer.Buffer, extra []Field) {
return
}

c.addTabIfNecessary(line)
c.addDelimiterIfNecessary(line)
line.AppendByte('{')
line.Write(context.buf.Bytes())
line.AppendByte('}')
}

func (c consoleEncoder) addTabIfNecessary(line *buffer.Buffer) {
func (c consoleEncoder) addDelimiterIfNecessary(line *buffer.Buffer) {
if line.Len() > 0 {
line.AppendByte('\t')
line.AppendByte(c.ElementDelimiter)
}
}
56 changes: 56 additions & 0 deletions zapcore/console_encoder_test.go
@@ -0,0 +1,56 @@
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package zapcore_test

import (
"github.com/stretchr/testify/assert"
. "go.uber.org/zap/zapcore"
"testing"
lixingwang marked this conversation as resolved.
Show resolved Hide resolved
)

func TestSetConsoleElementDelimiter(t *testing.T) {
lixingwang marked this conversation as resolved.
Show resolved Hide resolved
encodeConfig := humanEncoderConfig()
encodeConfig.ElementDelimiter = ' '
enc := NewConsoleEncoder(encodeConfig)
enc.AddString("str", "foo")

buf, _ := enc.EncodeEntry(Entry{
Message: "fake",
Level: DebugLevel,
}, nil)

lixingwang marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, `0001-01-01T00:00:00.000Z DEBUG fake {"str": "foo"}
`, buf.String())
buf.Free()

encodeConfig.ElementDelimiter = '-'
enc = NewConsoleEncoder(encodeConfig)
enc.AddString("str", "foo")

buf, _ = enc.EncodeEntry(Entry{
Message: "fake",
Level: DebugLevel,
}, nil)

assert.Equal(t, `0001-01-01T00:00:00.000Z-DEBUG-fake-{"str": "foo"}
`, buf.String())
buf.Free()

}
2 changes: 2 additions & 0 deletions zapcore/encoder.go
Expand Up @@ -239,6 +239,8 @@ type EncoderConfig struct {
// Unlike the other primitive type encoders, EncodeName is optional. The
// zero value falls back to FullNameEncoder.
EncodeName NameEncoder `json:"nameEncoder" yaml:"nameEncoder"`
//More on console, the delimiter of each element
lixingwang marked this conversation as resolved.
Show resolved Hide resolved
ElementDelimiter byte `json:"elementDelimiter" yaml:"elementDelimiter"`
}

// ObjectEncoder is a strongly-typed, encoding-agnostic interface for adding a
Expand Down