optimize: improve performance on log disabled (#3916)

master
Kevin Wan 9 months ago committed by GitHub
parent bfe8335cb2
commit 0f1d4c6bca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -17,14 +17,13 @@ import (
const callerDepth = 4 const callerDepth = 4
var ( var (
timeFormat = "2006-01-02T15:04:05.000Z07:00" timeFormat = "2006-01-02T15:04:05.000Z07:00"
logLevel uint32
encoding uint32 = jsonEncodingType encoding uint32 = jsonEncodingType
// maxContentLength is used to truncate the log content, 0 for not truncating. // maxContentLength is used to truncate the log content, 0 for not truncating.
maxContentLength uint32 maxContentLength uint32
// use uint32 for atomic operations // use uint32 for atomic operations
disableLog uint32
disableStat uint32 disableStat uint32
logLevel uint32
options logOptions options logOptions
writer = new(atomicWriter) writer = new(atomicWriter)
setupOnce sync.Once setupOnce sync.Once
@ -96,7 +95,7 @@ func Debugw(msg string, fields ...LogField) {
// Disable disables the logging. // Disable disables the logging.
func Disable() { func Disable() {
atomic.StoreUint32(&disableLog, 1) atomic.StoreUint32(&logLevel, disableLevel)
writer.Store(nopWriter{}) writer.Store(nopWriter{})
} }
@ -250,7 +249,7 @@ 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 { if atomic.LoadUint32(&logLevel) != disableLevel {
writer.Store(w) writer.Store(w)
} }
} }

@ -666,6 +666,7 @@ func TestDisable(t *testing.T) {
WithMaxSize(1024)(&opt) WithMaxSize(1024)(&opt)
assert.Nil(t, Close()) assert.Nil(t, Close())
assert.Nil(t, Close()) assert.Nil(t, Close())
assert.Equal(t, uint32(disableLevel), atomic.LoadUint32(&logLevel))
} }
func TestDisableStat(t *testing.T) { func TestDisableStat(t *testing.T) {
@ -680,7 +681,7 @@ func TestDisableStat(t *testing.T) {
} }
func TestSetWriter(t *testing.T) { func TestSetWriter(t *testing.T) {
atomic.StoreUint32(&disableLog, 0) atomic.StoreUint32(&logLevel, 0)
Reset() Reset()
SetWriter(nopWriter{}) SetWriter(nopWriter{})
assert.NotNil(t, writer.Load()) assert.NotNil(t, writer.Load())

@ -15,6 +15,8 @@ const (
ErrorLevel ErrorLevel
// SevereLevel only log severe messages // SevereLevel only log severe messages
SevereLevel SevereLevel
// disableLevel doesn't log any messages
disableLevel = 0xff
) )
const ( const (

Loading…
Cancel
Save