fix: logx disable not working in some cases (#2306)

* fix: logx disable not working in some cases

* fix: test fail
master
Kevin Wan 2 years ago committed by GitHub
parent 5d4ae201d0
commit 2d60f0c65a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -21,6 +21,7 @@ var (
logLevel uint32 logLevel uint32
encoding uint32 = jsonEncodingType encoding uint32 = jsonEncodingType
// use uint32 for atomic operations // use uint32 for atomic operations
disableLog uint32
disableStat uint32 disableStat uint32
options logOptions options logOptions
writer = new(atomicWriter) writer = new(atomicWriter)
@ -73,6 +74,7 @@ func Close() error {
// Disable disables the logging. // Disable disables the logging.
func Disable() { func Disable() {
atomic.StoreUint32(&disableLog, 1)
writer.Store(nopWriter{}) writer.Store(nopWriter{})
} }
@ -201,7 +203,9 @@ func SetLevel(level uint32) {
// SetWriter sets the logging writer. It can be used to customize the logging. // SetWriter sets the logging writer. It can be used to customize the logging.
func SetWriter(w Writer) { func SetWriter(w Writer) {
if atomic.LoadUint32(&disableLog) == 0 {
writer.Store(w) writer.Store(w)
}
} }
// SetUp sets up the logx. If already set up, just return nil. // SetUp sets up the logx. If already set up, just return nil.

@ -599,6 +599,7 @@ func TestDisableStat(t *testing.T) {
} }
func TestSetWriter(t *testing.T) { func TestSetWriter(t *testing.T) {
atomic.StoreUint32(&disableLog, 0)
Reset() Reset()
SetWriter(nopWriter{}) SetWriter(nopWriter{})
assert.NotNil(t, writer.Load()) assert.NotNil(t, writer.Load())

@ -20,10 +20,6 @@ import (
var errDummy = errors.New("dummy") var errDummy = errors.New("dummy")
func init() {
logx.Disable()
}
func TestKeepPromise_accept(t *testing.T) { func TestKeepPromise_accept(t *testing.T) {
p := new(mockPromise) p := new(mockPromise)
kp := keepablePromise{ kp := keepablePromise{

@ -17,10 +17,6 @@ import (
var errDummy = errors.New("dummy") var errDummy = errors.New("dummy")
func init() {
logx.Disable()
}
func TestKeepPromise_accept(t *testing.T) { func TestKeepPromise_accept(t *testing.T) {
p := new(mockPromise) p := new(mockPromise)
kp := keepablePromise{ kp := keepablePromise{

Loading…
Cancel
Save