diff --git a/core/logx/logs.go b/core/logx/logs.go index 817cb557..0c049324 100644 --- a/core/logx/logs.go +++ b/core/logx/logs.go @@ -17,14 +17,13 @@ import ( const callerDepth = 4 var ( - timeFormat = "2006-01-02T15:04:05.000Z07:00" - logLevel uint32 + timeFormat = "2006-01-02T15:04:05.000Z07:00" encoding uint32 = jsonEncodingType // maxContentLength is used to truncate the log content, 0 for not truncating. maxContentLength uint32 // use uint32 for atomic operations - disableLog uint32 disableStat uint32 + logLevel uint32 options logOptions writer = new(atomicWriter) setupOnce sync.Once @@ -96,7 +95,7 @@ func Debugw(msg string, fields ...LogField) { // Disable disables the logging. func Disable() { - atomic.StoreUint32(&disableLog, 1) + atomic.StoreUint32(&logLevel, disableLevel) writer.Store(nopWriter{}) } @@ -250,7 +249,7 @@ func SetLevel(level uint32) { // SetWriter sets the logging writer. It can be used to customize the logging. func SetWriter(w Writer) { - if atomic.LoadUint32(&disableLog) == 0 { + if atomic.LoadUint32(&logLevel) != disableLevel { writer.Store(w) } } diff --git a/core/logx/logs_test.go b/core/logx/logs_test.go index 0b37960f..818f459e 100644 --- a/core/logx/logs_test.go +++ b/core/logx/logs_test.go @@ -666,6 +666,7 @@ func TestDisable(t *testing.T) { WithMaxSize(1024)(&opt) assert.Nil(t, Close()) assert.Nil(t, Close()) + assert.Equal(t, uint32(disableLevel), atomic.LoadUint32(&logLevel)) } func TestDisableStat(t *testing.T) { @@ -680,7 +681,7 @@ func TestDisableStat(t *testing.T) { } func TestSetWriter(t *testing.T) { - atomic.StoreUint32(&disableLog, 0) + atomic.StoreUint32(&logLevel, 0) Reset() SetWriter(nopWriter{}) assert.NotNil(t, writer.Load()) diff --git a/core/logx/vars.go b/core/logx/vars.go index 59688e87..90ef3628 100644 --- a/core/logx/vars.go +++ b/core/logx/vars.go @@ -15,6 +15,8 @@ const ( ErrorLevel // SevereLevel only log severe messages SevereLevel + // disableLevel doesn't log any messages + disableLevel = 0xff ) const (