From 53a387079b46cce51bbb6d360d9c57f86977addb Mon Sep 17 00:00:00 2001 From: Tracy Li Date: Fri, 17 Jul 2020 17:00:00 -0500 Subject: [PATCH] Support configurable delimiter for console encoder (#697) 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. --- zapcore/console_encoder.go | 14 +++-- zapcore/console_encoder_test.go | 91 +++++++++++++++++++++++++++++++++ zapcore/encoder.go | 3 ++ 3 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 zapcore/console_encoder_test.go diff --git a/zapcore/console_encoder.go b/zapcore/console_encoder.go index b7875966f..54aae7ef9 100644 --- a/zapcore/console_encoder.go +++ b/zapcore/console_encoder.go @@ -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)} } @@ -94,7 +98,7 @@ 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]) } @@ -102,7 +106,7 @@ func (c consoleEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer, // Add the message itself. if c.MessageKey != "" { - c.addTabIfNecessary(line) + c.addSeparatorIfNecessary(line) line.AppendString(ent.Message) } @@ -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) } } diff --git a/zapcore/console_encoder_test.go b/zapcore/console_encoder_test.go new file mode 100644 index 000000000..e366f0149 --- /dev/null +++ b/zapcore/console_encoder_test.go @@ -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 +} diff --git a/zapcore/encoder.go b/zapcore/encoder.go index db97aa332..961878c46 100644 --- a/zapcore/encoder.go +++ b/zapcore/encoder.go @@ -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