From 51377a8ffb969cc2c7f4738bee27bb0191a3df0c Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Wed, 27 Dec 2017 14:37:53 +0100 Subject: [PATCH] logger: support replacing fields do no delete() from array since this could lead to resizing refs #10 --- logger/logger.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/logger/logger.go b/logger/logger.go index 618c18f..716dfc2 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -103,12 +103,17 @@ func (l *Logger) WithOutlet(outlet Outlet, level Level) *Logger { return child } +func (l *Logger) ReplaceField(field string, val interface{}) *Logger { + l.fields[field] = nil + return l.WithField(field, val) +} + func (l *Logger) WithField(field string, val interface{}) *Logger { l.mtx.Lock() defer l.mtx.Unlock() - if _, ok := l.fields[field]; ok { + if val, ok := l.fields[field]; ok && val != nil { l.logInternalError(nil, fmt.Sprintf("caller overwrites field '%s'. Stack: %s", field, string(debug.Stack()))) }