Skip to content

Commit

Permalink
Fix race
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly committed Oct 2, 2024
1 parent 63ad039 commit b8c31bd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 3 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,9 @@ func setLogLevel(lvl Level, logChange bool) Level {
logUnconditionalf(Config.LogFileAndLine, Info, "Log level is now %d %s (was %d %s)", lvl, lvl.String(), prev, prev.String())
}
setLevel(lvl)
jWriter.mutex.Lock()
Config.Level = lvl.String()
jWriter.mutex.Unlock()
}
return prev
}
Expand Down Expand Up @@ -368,6 +370,7 @@ func Logf(lvl Level, format string, rest ...interface{}) {
}

// Used when doing our own logging writing, in JSON/structured mode (and some color variants as well, misnomer).
// Also reusing that lock to update global Config.Level.
var (
jWriter = jsonWriter{w: os.Stderr, tsBuf: make([]byte, 0, 32)}
)
Expand Down
7 changes: 4 additions & 3 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -963,13 +963,14 @@ func TestConcurrentLevelSet(t *testing.T) {
var wg sync.WaitGroup
wg.Add(int(Fatal - Verbose))
for i := Verbose; i < Fatal; i++ {
go func() {
SetLogLevel(i)
go func(lvl Level) {
SetLogLevel(lvl)
wg.Done()
}()
}(i)
}
wg.Wait()
SetLogLevel(Info)
t.Logf("log level is now %s", GetLogLevel().String())
}

// --- Benchmarks
Expand Down

0 comments on commit b8c31bd

Please sign in to comment.