From 49196765e53f20d4bae1b0eee8ac2bad1d7192a6 Mon Sep 17 00:00:00 2001 From: Vinay P Date: Wed, 24 Jun 2020 09:51:52 +0100 Subject: [PATCH] Separate hasValuers for prefix and suffix conveys intent much more clearly, and binds suffix values only when needed. --- log/log.go | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/log/log.go b/log/log.go index 8549e354a..9241be3c8 100644 --- a/log/log.go +++ b/log/log.go @@ -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, } } @@ -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, } } @@ -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), } } @@ -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 { @@ -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...)