logger: support replacing fields

do no delete() from array since this could lead to resizing

refs #10
This commit is contained in:
Christian Schwarz 2017-12-27 14:37:53 +01:00
parent f14dc3107f
commit 51377a8ffb

View File

@ -103,12 +103,17 @@ func (l *Logger) WithOutlet(outlet Outlet, level Level) *Logger {
return child 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 { func (l *Logger) WithField(field string, val interface{}) *Logger {
l.mtx.Lock() l.mtx.Lock()
defer l.mtx.Unlock() defer l.mtx.Unlock()
if _, ok := l.fields[field]; ok { if val, ok := l.fields[field]; ok && val != nil {
l.logInternalError(nil, l.logInternalError(nil,
fmt.Sprintf("caller overwrites field '%s'. Stack: %s", field, string(debug.Stack()))) fmt.Sprintf("caller overwrites field '%s'. Stack: %s", field, string(debug.Stack())))
} }