From 8c7213663104a9a1788b3477f06852484eb46a4f Mon Sep 17 00:00:00 2001 From: Archer Date: Sun, 28 Aug 2022 22:10:50 +0800 Subject: [PATCH] make logx#getWriter concurrency-safe (#2233) * make logx#getWriter concurrency-safe * make logx#getWriter concurrency-safe --- core/logx/logs.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/core/logx/logs.go b/core/logx/logs.go index 76e75571..9af80757 100644 --- a/core/logx/logs.go +++ b/core/logx/logs.go @@ -358,10 +358,19 @@ func errorTextSync(msg string) { } func getWriter() Writer { - w := writer.Load() + var w Writer + + writer.lock.RLock() + w = writer.writer + writer.lock.RUnlock() + if w == nil { - w = newConsoleWriter() - writer.Store(w) + writer.lock.Lock() + if writer.writer == nil { + writer.writer = newConsoleWriter() + } + w = writer.writer + writer.lock.Unlock() } return w