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

Make API for omitting keys more approachable. #725

Merged
merged 2 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion zapcore/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import (
// behavior.
const DefaultLineEnding = "\n"

// OmitKey defines the key to use when callers want to remove a key from log output.
const OmitKey = ""

// A LevelEncoder serializes a Level to a primitive type.
type LevelEncoder func(Level, PrimitiveArrayEncoder)

Expand Down Expand Up @@ -343,6 +346,7 @@ type Encoder interface {
Clone() Encoder

// EncodeEntry encodes an entry and fields, along with any accumulated
// context, into a byte buffer and returns it.
// context, into a byte buffer and returns it. Any fields that are empty,
// including fields on the `Entry` type, should be omitted.
EncodeEntry(Entry, []Field) (*buffer.Buffer, error)
}
12 changes: 6 additions & 6 deletions zapcore/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestEncoderConfiguration(t *testing.T) {
{
desc: "skip level if LevelKey is omitted",
cfg: EncoderConfig{
LevelKey: "",
LevelKey: OmitKey,
TimeKey: "T",
MessageKey: "M",
NameKey: "N",
Expand All @@ -140,7 +140,7 @@ func TestEncoderConfiguration(t *testing.T) {
desc: "skip timestamp if TimeKey is omitted",
cfg: EncoderConfig{
LevelKey: "L",
TimeKey: "",
TimeKey: OmitKey,
MessageKey: "M",
NameKey: "N",
CallerKey: "C",
Expand All @@ -159,7 +159,7 @@ func TestEncoderConfiguration(t *testing.T) {
cfg: EncoderConfig{
LevelKey: "L",
TimeKey: "T",
MessageKey: "",
MessageKey: OmitKey,
NameKey: "N",
CallerKey: "C",
StacktraceKey: "S",
Expand All @@ -178,7 +178,7 @@ func TestEncoderConfiguration(t *testing.T) {
LevelKey: "L",
TimeKey: "T",
MessageKey: "M",
NameKey: "",
NameKey: OmitKey,
CallerKey: "C",
StacktraceKey: "S",
LineEnding: base.LineEnding,
Expand All @@ -197,7 +197,7 @@ func TestEncoderConfiguration(t *testing.T) {
TimeKey: "T",
MessageKey: "M",
NameKey: "N",
CallerKey: "",
CallerKey: OmitKey,
StacktraceKey: "S",
LineEnding: base.LineEnding,
EncodeTime: base.EncodeTime,
Expand All @@ -216,7 +216,7 @@ func TestEncoderConfiguration(t *testing.T) {
MessageKey: "M",
NameKey: "N",
CallerKey: "C",
StacktraceKey: "",
StacktraceKey: OmitKey,
LineEnding: base.LineEnding,
EncodeTime: base.EncodeTime,
EncodeDuration: base.EncodeDuration,
Expand Down
3 changes: 2 additions & 1 deletion zapcore/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ func (ec EntryCaller) TrimmedPath() string {

// An Entry represents a complete log message. The entry's structured context
// is already serialized, but the log level, time, message, and call site
// information are available for inspection and modification.
// information are available for inspection and modification. Any fields left
// empty will be omitted when encoding.
//
// Entries are pooled, so any functions that accept them MUST be careful not to
// retain references to them.
Expand Down