Skip to content

Commit

Permalink
fix: lock buffer during write before read
Browse files Browse the repository at this point in the history
The formatters write to the internal buffer. We need to guard that
before we read the buffer.

Fixes: #67
  • Loading branch information
aymanbagabas committed Jul 28, 2023
1 parent 280c4e3 commit 755c7dd
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ func (l *Logger) log(level Level, msg interface{}, keyvals ...interface{}) {
return
}

l.mu.Lock()
defer l.mu.Unlock()
defer l.b.Reset()

var kvs []interface{}
Expand Down Expand Up @@ -98,6 +96,7 @@ func (l *Logger) log(level Level, msg interface{}, keyvals ...interface{}) {
kvs = append(kvs, ErrMissingValue)
}

l.mu.Lock()
switch l.formatter {
case LogfmtFormatter:
l.logfmtFormatter(kvs...)
Expand All @@ -106,6 +105,7 @@ func (l *Logger) log(level Level, msg interface{}, keyvals ...interface{}) {
default:
l.textFormatter(kvs...)
}
l.mu.Unlock()

_, _ = l.w.Write(l.b.Bytes())
}
Expand Down

0 comments on commit 755c7dd

Please sign in to comment.