Skip to content

Commit

Permalink
Support configurable delimiter for console encoder (#697)
Browse files Browse the repository at this point in the history
It would be nice to have a configurable delimiter for console encoder, tab by default.

For our cases, we prefer to have space as element delimiter.

A custom console delimiter can be set through the `consoleSeparator` encoder configuration.
  • Loading branch information
lixingwang authored Jul 17, 2020
1 parent 275c926 commit 53a3870
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 5 deletions.
14 changes: 9 additions & 5 deletions zapcore/console_encoder.go
Original file line number Diff line number Diff line change
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 len(cfg.ConsoleSeparator) == 0 {
// Use a default delimiter of '\t' for backwards compatibility
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.AppendString(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.AppendString(c.ConsoleSeparator)
}
}
91 changes: 91 additions & 0 deletions zapcore/console_encoder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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 string
wantConsole string
}{
{
desc: "space console separator",
separator: " ",
wantConsole: "0 info main foo.go:42 hello\nfake-stack\n",
},
{
desc: "default console separator",
separator: "",
wantConsole: "0\tinfo\tmain\tfoo.go:42\thello\nfake-stack\n",
},
{
desc: "tag 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)
if !assert.NoError(t, err) {
return
}
assert.Equal(
t,
tt.wantConsole,
consoleOut.String(),
"Unexpected console output",
)
})

}
}

func encoderTestEncoderConfig(separator string) EncoderConfig {
testEncoder := testEncoderConfig()
testEncoder.ConsoleSeparator = separator
return testEncoder
}
3 changes: 3 additions & 0 deletions zapcore/encoder.go
Original file line number Diff line number Diff line change
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 string `json:"consoleSeparator" yaml:"consoleSeparator"`
}

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

0 comments on commit 53a3870

Please sign in to comment.