fix(logx): need to wait for the first caller to complete the execution. (#2213)

master
chen quan 2 years ago committed by GitHub
parent 3f3c811e08
commit 4fad067a0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,6 +7,7 @@ import (
"os" "os"
"path" "path"
"runtime/debug" "runtime/debug"
"sync"
"sync/atomic" "sync/atomic"
"time" "time"
@ -23,7 +24,7 @@ var (
disableStat uint32 disableStat uint32
options logOptions options logOptions
writer = new(atomicWriter) writer = new(atomicWriter)
setupOnce uint32 setupOnce sync.Once
) )
type ( type (
@ -206,13 +207,11 @@ func SetWriter(w Writer) {
// SetUp sets up the logx. If already set up, just return nil. // SetUp sets up the logx. If already set up, just return nil.
// we allow SetUp to be called multiple times, because for example // we allow SetUp to be called multiple times, because for example
// we need to allow different service frameworks to initialize logx respectively. // we need to allow different service frameworks to initialize logx respectively.
func SetUp(c LogConf) error { func SetUp(c LogConf) (err error) {
// Just ignore the subsequent SetUp calls. // Just ignore the subsequent SetUp calls.
// Because multiple services in one process might call SetUp respectively. // Because multiple services in one process might call SetUp respectively.
if !atomic.CompareAndSwapUint32(&setupOnce, 0, 1) { // Need to wait for the first caller to complete the execution.
return nil setupOnce.Do(func() {
}
setupLogLevel(c) setupLogLevel(c)
if len(c.TimeFormat) > 0 { if len(c.TimeFormat) > 0 {
@ -228,13 +227,15 @@ func SetUp(c LogConf) error {
switch c.Mode { switch c.Mode {
case fileMode: case fileMode:
return setupWithFiles(c) err = setupWithFiles(c)
case volumeMode: case volumeMode:
return setupWithVolume(c) err = setupWithVolume(c)
default: default:
setupWithConsole() setupWithConsole()
return nil
} }
})
return
} }
// Severe writes v into severe log. // Severe writes v into severe log.

Loading…
Cancel
Save