Skip to content

Commit 959d342

Browse files
committed
Prevent data race in SetOutput* methods
1 parent ab4d90c commit 959d342

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

klog.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,8 @@ func (rb *redirectBuffer) Write(bytes []byte) (n int, err error) {
746746

747747
// SetOutput sets the output destination for all severities
748748
func SetOutput(w io.Writer) {
749+
logging.mu.Lock()
750+
defer logging.mu.Unlock()
749751
for s := fatalLog; s >= infoLog; s-- {
750752
rb := &redirectBuffer{
751753
w: w,
@@ -756,6 +758,8 @@ func SetOutput(w io.Writer) {
756758

757759
// SetOutputBySeverity sets the output destination for specific severity
758760
func SetOutputBySeverity(name string, w io.Writer) {
761+
logging.mu.Lock()
762+
defer logging.mu.Unlock()
759763
sev, ok := severityByName(name)
760764
if !ok {
761765
panic(fmt.Sprintf("SetOutputBySeverity(%q): unrecognized severity name", name))

0 commit comments

Comments
 (0)