diff --git a/core/logx/logs.go b/core/logx/logs.go index 9af80757..325c9370 100644 --- a/core/logx/logs.go +++ b/core/logx/logs.go @@ -358,19 +358,9 @@ func errorTextSync(msg string) { } func getWriter() Writer { - var w Writer - - writer.lock.RLock() - w = writer.writer - writer.lock.RUnlock() - + w := writer.Load() if w == nil { - writer.lock.Lock() - if writer.writer == nil { - writer.writer = newConsoleWriter() - } - w = writer.writer - writer.lock.Unlock() + w = writer.StoreIfNil(newConsoleWriter()) } return w diff --git a/core/logx/writer.go b/core/logx/writer.go index cb0c121f..08873c09 100644 --- a/core/logx/writer.go +++ b/core/logx/writer.go @@ -67,6 +67,17 @@ func (w *atomicWriter) Store(v Writer) { w.writer = v } +func (w *atomicWriter) StoreIfNil(v Writer) Writer { + w.lock.Lock() + defer w.lock.Unlock() + + if w.writer == nil { + w.writer = v + } + + return w.writer +} + func (w *atomicWriter) Swap(v Writer) Writer { w.lock.Lock() defer w.lock.Unlock()