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

Leveled fields: Fields present only at certain log levels #1251

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions example_test.go
Expand Up @@ -358,3 +358,23 @@ func ExampleWrapCore_wrap() {
// {"level":"info","msg":"doubled"}
// {"level":"info","msg":"doubled"}
}

func ExampleDebugField() {
logger := zap.NewExample()
defer logger.Sync()

logger.Info(".Info call filters out the debug field",
zap.Int("IntKey", 1),
zap.DebugField(zap.String("Key", "value")),
zap.Duration("DurationKey", time.Second),
)
logger.Debug(".Debug call does NOT filter out the debug field",
zap.Int("IntKey", 1),
zap.DebugField(zap.String("Key", "value")),
zap.Duration("DurationKey", time.Second),
)

// Output:
// {"level":"info","msg":".Info call filters out the debug field","IntKey":1,"DurationKey":"1s"}
// {"level":"debug","msg":".Debug call does NOT filter out the debug field","IntKey":1,"Key":"value","DurationKey":"1s"}
}
9 changes: 9 additions & 0 deletions field.go
Expand Up @@ -35,8 +35,17 @@ type Field = zapcore.Field
var (
_minTimeInt64 = time.Unix(0, math.MinInt64)
_maxTimeInt64 = time.Unix(0, math.MaxInt64)

debugLevelField = zapcore.DebugLevel
)

// DebugField wraps a field so that DebugLevel log displays it.
// See https://github.com/uber-go/zap/issues/1078 for motivation.
func DebugField(f Field) Field {
f.Level = &debugLevelField
return f
}

// Skip constructs a no-op field, which is often useful when handling invalid
// inputs in other Field constructors.
func Skip() Field {
Expand Down
15 changes: 13 additions & 2 deletions zapcore/entry.go
Expand Up @@ -249,8 +249,19 @@ func (ce *CheckedEntry) Write(fields ...Field) {
ce.dirty = true

var err error

// https://github.com/golang/go/wiki/SliceTricks#filtering-without-allocating
filteredFields := fields[:0]
for _, field := range fields {
// include the field if the level is unset or
// the field level is higher than the entry's level.
if field.Level == nil || *field.Level >= ce.Entry.Level {
filteredFields = append(filteredFields, field)
}
}

for i := range ce.cores {
err = multierr.Append(err, ce.cores[i].Write(ce.Entry, fields))
err = multierr.Append(err, ce.cores[i].Write(ce.Entry, filteredFields))
}
if err != nil && ce.ErrorOutput != nil {
fmt.Fprintf(ce.ErrorOutput, "%v write error: %v\n", ce.Time, err)
Expand All @@ -259,7 +270,7 @@ func (ce *CheckedEntry) Write(fields ...Field) {

hook := ce.after
if hook != nil {
hook.OnWrite(ce, fields)
hook.OnWrite(ce, filteredFields)
}
putCheckedEntry(ce)
}
Expand Down
1 change: 1 addition & 0 deletions zapcore/field.go
Expand Up @@ -106,6 +106,7 @@ type Field struct {
Type FieldType
Integer int64
String string
Level *Level
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making this attribute a pointer seemed the cleanest way to implement this - doing so would make all existing fields' Level attribute be nil.

Interface interface{}
}

Expand Down