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
14 changes: 9 additions & 5 deletions zapcore/console_encoder.go
Expand Up @@ -56,6 +56,10 @@ 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.ConsoleSeparator == 0 {
// Default delimiter '\t'
lixingwang marked this conversation as resolved.
Show resolved Hide resolved
cfg.ConsoleSeparator = '\t'
}
return consoleEncoder{newJSONEncoder(cfg, true)}
}

Expand Down Expand Up @@ -94,15 +98,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.ConsoleSeparator)
}
fmt.Fprint(line, arr.elems[i])
}
putSliceEncoder(arr)

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

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

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

func (c consoleEncoder) addTabIfNecessary(line *buffer.Buffer) {
func (c consoleEncoder) addSeparatorIfNecessary(line *buffer.Buffer) {
if line.Len() > 0 {
line.AppendByte('\t')
line.AppendByte(c.ConsoleSeparator)
}
}
89 changes: 89 additions & 0 deletions zapcore/console_encoder_test.go
@@ -0,0 +1,89 @@
// 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 (
"testing"

"github.com/stretchr/testify/assert"
. "go.uber.org/zap/zapcore"
)

var (
testEntry = Entry{
LoggerName: "main",
Level: InfoLevel,
Message: `hello`,
Time: _epoch,
Stack: "fake-stack",
Caller: EntryCaller{Defined: true, File: "foo.go", Line: 42},
}
)

func TestConsoleSeparator(t *testing.T) {
tests := []struct {
desc string
separator byte
wantConsole string
}{
{
desc: "space console separator",
separator: ' ',
wantConsole: "0 info main foo.go:42 hello\nfake-stack\n",
},
{
desc: "default console separator",
separator: ' ',
lixingwang marked this conversation as resolved.
Show resolved Hide resolved
wantConsole: "0\tinfo\tmain\tfoo.go:42\thello\nfake-stack\n",
},
{
desc: "default console separator",
separator: '\t',
wantConsole: "0\tinfo\tmain\tfoo.go:42\thello\nfake-stack\n",
},
{
desc: "dash console separator",
separator: '-',
wantConsole: "0-info-main-foo.go:42-hello\nfake-stack\n",
},
}

for _, tt := range tests {
console := NewConsoleEncoder(encoderTestEncoderConfig(tt.separator))
t.Run(tt.desc, func(t *testing.T) {
entry := testEntry
consoleOut, err := console.EncodeEntry(entry, nil)
assert.Nil(t, err)
lixingwang marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(
t,
tt.wantConsole,
consoleOut.String(),
"Unexpected console output: expected to %v.", tt.desc,
lixingwang marked this conversation as resolved.
Show resolved Hide resolved
)
})

}
}

func encoderTestEncoderConfig(separator byte) EncoderConfig {
testEncoder := testEncoderConfig()
testEncoder.ConsoleSeparator = separator
return testEncoder
}
3 changes: 3 additions & 0 deletions zapcore/encoder.go
Expand Up @@ -329,6 +329,9 @@ 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"`
// Configures the field separator used by the console encoder. Defaults
// to tab.
ConsoleSeparator byte `json:"consoleSeparator" yaml:"consoleSeparator"`
lixingwang marked this conversation as resolved.
Show resolved Hide resolved
}

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