Skip to content

Commit

Permalink
SetLogger/ClearLogger/SetFilter: document as not thread-safe
Browse files Browse the repository at this point in the history
It has never been claimed that these functions are thread-safe and in practice
they weren't because read access was not protected by the same mutex as the
write access.

These functions were meant to be called during program initialization,
therefore the documentation is lacking, not the implementation.

Making them thread-safe would imply adding mutex locking to the implementation
of V, which most likely would have a noticeable effect.
  • Loading branch information
pohly committed Feb 8, 2022
1 parent d78dad3 commit 714cb7a
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions klog.go
Expand Up @@ -965,15 +965,18 @@ func (rb *redirectBuffer) Write(bytes []byte) (n int, err error) {
//
// To remove a backing logr implemention, use ClearLogger. Setting an
// empty logger with SetLogger(logr.Logger{}) does not work.
//
// Modifying the logger is not thread-safe and should be done while no other
// goroutines invoke log calls, usually during program initialization.
func SetLogger(logr logr.Logger) {
logging.mu.Lock()
defer logging.mu.Unlock()

logging.logr = &logr
}

// ClearLogger removes a backing logr implementation if one was set earlier
// with SetLogger.
//
// Modifying the logger is not thread-safe and should be done while no other
// goroutines invoke log calls, usually during program initialization.
func ClearLogger() {
logging.mu.Lock()
defer logging.mu.Unlock()
Expand Down Expand Up @@ -1775,10 +1778,11 @@ type LogFilter interface {
FilterS(msg string, keysAndValues []interface{}) (string, []interface{})
}

// SetLogFilter installs a filter that is used for all log calls.
//
// Modifying the filter is not thread-safe and should be done while no other
// goroutines invoke log calls, usually during program initialization.
func SetLogFilter(filter LogFilter) {
logging.mu.Lock()
defer logging.mu.Unlock()

logging.filter = filter
}

Expand Down

0 comments on commit 714cb7a

Please sign in to comment.