Skip to content

Commit

Permalink
Added SkipLineEnding to zapcore.EncoderConfig (#989)
Browse files Browse the repository at this point in the history
* Added SkipLineEnding to zapcore.EncoderConfig
* zapcore: LineEnding logic moved to newJSONEncoder
  • Loading branch information
lruggieri authored Nov 9, 2021
1 parent b62116b commit 7a9e717
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
6 changes: 1 addition & 5 deletions zapcore/console_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,7 @@ func (c consoleEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer,
line.AppendString(ent.Stack)
}

if c.LineEnding != "" {
line.AppendString(c.LineEnding)
} else {
line.AppendString(DefaultLineEnding)
}
line.AppendString(c.LineEnding)
return line, nil
}

Expand Down
17 changes: 9 additions & 8 deletions zapcore/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,15 @@ func (e *NameEncoder) UnmarshalText(text []byte) error {
type EncoderConfig struct {
// Set the keys used for each log entry. If any key is empty, that portion
// of the entry is omitted.
MessageKey string `json:"messageKey" yaml:"messageKey"`
LevelKey string `json:"levelKey" yaml:"levelKey"`
TimeKey string `json:"timeKey" yaml:"timeKey"`
NameKey string `json:"nameKey" yaml:"nameKey"`
CallerKey string `json:"callerKey" yaml:"callerKey"`
FunctionKey string `json:"functionKey" yaml:"functionKey"`
StacktraceKey string `json:"stacktraceKey" yaml:"stacktraceKey"`
LineEnding string `json:"lineEnding" yaml:"lineEnding"`
MessageKey string `json:"messageKey" yaml:"messageKey"`
LevelKey string `json:"levelKey" yaml:"levelKey"`
TimeKey string `json:"timeKey" yaml:"timeKey"`
NameKey string `json:"nameKey" yaml:"nameKey"`
CallerKey string `json:"callerKey" yaml:"callerKey"`
FunctionKey string `json:"functionKey" yaml:"functionKey"`
StacktraceKey string `json:"stacktraceKey" yaml:"stacktraceKey"`
SkipLineEnding bool `json:"skipLineEnding" yaml:"skipLineEnding"`
LineEnding string `json:"lineEnding" yaml:"lineEnding"`
// Configure the primitive representations of common complex types. For
// example, some users may want all time.Times serialized as floating-point
// seconds since epoch, while others may prefer ISO8601 strings.
Expand Down
20 changes: 20 additions & 0 deletions zapcore/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ func TestEncoderConfiguration(t *testing.T) {
expectedJSON: `{"L":"info","T":0,"N":"main","C":"foo.go:42","F":"foo.Foo","M":"hello","S":"fake-stack"}` + "\n",
expectedConsole: "0\tinfo\tmain\tfoo.go:42\tfoo.Foo\thello\nfake-stack\n",
},
{
desc: "skip line ending if SkipLineEnding is 'true'",
cfg: EncoderConfig{
LevelKey: "L",
TimeKey: "T",
MessageKey: "M",
NameKey: "N",
CallerKey: "C",
FunctionKey: "F",
StacktraceKey: "S",
LineEnding: base.LineEnding,
SkipLineEnding: true,
EncodeTime: base.EncodeTime,
EncodeDuration: base.EncodeDuration,
EncodeLevel: base.EncodeLevel,
EncodeCaller: base.EncodeCaller,
},
expectedJSON: `{"L":"info","T":0,"N":"main","C":"foo.go:42","F":"foo.Foo","M":"hello","S":"fake-stack"}`,
expectedConsole: "0\tinfo\tmain\tfoo.go:42\tfoo.Foo\thello\nfake-stack",
},
{
desc: "skip level if LevelKey is omitted",
cfg: EncoderConfig{
Expand Down
12 changes: 7 additions & 5 deletions zapcore/json_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ func NewJSONEncoder(cfg EncoderConfig) Encoder {
}

func newJSONEncoder(cfg EncoderConfig, spaced bool) *jsonEncoder {
if cfg.SkipLineEnding {
cfg.LineEnding = ""
} else if cfg.LineEnding == "" {
cfg.LineEnding = DefaultLineEnding
}

return &jsonEncoder{
EncoderConfig: &cfg,
buf: bufferpool.Get(),
Expand Down Expand Up @@ -418,11 +424,7 @@ func (enc *jsonEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer,
final.AddString(final.StacktraceKey, ent.Stack)
}
final.buf.AppendByte('}')
if final.LineEnding != "" {
final.buf.AppendString(final.LineEnding)
} else {
final.buf.AppendString(DefaultLineEnding)
}
final.buf.AppendString(final.LineEnding)

ret := final.buf
putJSONEncoder(final)
Expand Down

0 comments on commit 7a9e717

Please sign in to comment.