Skip to content

Commit

Permalink
Separate hasValuers for prefix and suffix
Browse files Browse the repository at this point in the history
conveys intent much more clearly, and
binds suffix values only when needed.
  • Loading branch information
Vinay P committed Jun 24, 2020
1 parent 0b66f30 commit 6746fef
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ func With(logger Logger, keyvals ...interface{}) Logger {
// backing array is created if the slice must grow in Log or With.
// Using the extra capacity without copying risks a data race that
// would violate the Logger interface contract.
keyvals: kvs[:len(kvs):len(kvs)],
sKeyvals: l.sKeyvals,
hasValuer: l.hasValuer || containsValuer(keyvals),
keyvals: kvs[:len(kvs):len(kvs)],
hasValuer: l.hasValuer || containsValuer(keyvals),
sKeyvals: l.sKeyvals,
sHasValuer: l.sHasValuer,
}
}

Expand Down Expand Up @@ -68,10 +69,11 @@ func WithPrefix(logger Logger, keyvals ...interface{}) Logger {
}
kvs = append(kvs, l.keyvals...)
return &context{
logger: l.logger,
keyvals: kvs,
sKeyvals: l.sKeyvals,
hasValuer: l.hasValuer || containsValuer(keyvals),
logger: l.logger,
keyvals: kvs,
hasValuer: l.hasValuer || containsValuer(keyvals),
sKeyvals: l.sKeyvals,
sHasValuer: l.sHasValuer,
}
}

Expand Down Expand Up @@ -101,10 +103,11 @@ func WithSuffix(logger Logger, keyvals ...interface{}) Logger {
}
kvs = append(l.sKeyvals, kvs...)
return &context{
logger: l.logger,
keyvals: l.keyvals,
sKeyvals: kvs,
hasValuer: l.hasValuer || containsValuer(keyvals),
logger: l.logger,
keyvals: l.keyvals,
hasValuer: l.hasValuer,
sKeyvals: kvs,
sHasValuer: l.sHasValuer || containsValuer(keyvals),
}
}

Expand All @@ -128,10 +131,12 @@ func WithSuffix(logger Logger, keyvals ...interface{}) Logger {
// returning a newly constructed context with a merged keyvals rather
// than simply wrapping the existing context.
type context struct {
logger Logger
keyvals []interface{}
sKeyvals []interface{}
hasValuer bool
logger Logger
keyvals []interface{}
hasValuer bool
// suffixes
sKeyvals []interface{}
sHasValuer bool
}

func newContext(logger Logger) *context {
Expand All @@ -149,15 +154,16 @@ func (l *context) Log(keyvals ...interface{}) error {
if len(kvs)%2 != 0 {
kvs = append(kvs, ErrMissingValue)
}
kvs = append(kvs, l.sKeyvals...)
if l.hasValuer {
// If no keyvals were appended above then we must copy l.keyvals
// and l.sKeyvals so that future log events will reevaluate
// the stored Valuers.
// If no keyvals were appended above then we must copy l.keyvals so
// that future log events will reevaluate the stored Valuers.
if len(keyvals) == 0 {
kvs = append([]interface{}{}, append(l.keyvals, l.sKeyvals...)...)
kvs = append([]interface{}{}, l.keyvals...)
}
bindValues(kvs[:(len(l.keyvals))])
}
kvs = append(kvs, l.sKeyvals...)
if l.sHasValuer {
bindValues(kvs[len(kvs) - len(l.sKeyvals):])
}
return l.logger.Log(kvs...)
Expand Down

0 comments on commit 6746fef

Please sign in to comment.