From 233f2bb7c1077bf80331e5b214b8a1d58f2ba293 Mon Sep 17 00:00:00 2001 From: DevinZeng Date: Thu, 23 Jan 2025 16:15:33 +0800 Subject: [PATCH] implement #4595, add logx.GetLevel() --- core/logc/logs.go | 5 +++++ core/logc/logs_test.go | 2 ++ core/logx/logs.go | 5 +++++ core/logx/logs_test.go | 6 ++++++ 4 files changed, 18 insertions(+) diff --git a/core/logc/logs.go b/core/logc/logs.go index b8343fa26ce8..4cc2338a3bcc 100644 --- a/core/logc/logs.go +++ b/core/logc/logs.go @@ -108,6 +108,11 @@ func SetLevel(level uint32) { logx.SetLevel(level) } +// GetLevel gets the logging level. +func GetLevel() uint32 { + return logx.GetLevel() +} + // SetUp sets up the logx. // If already set up, return nil. // We allow SetUp to be called multiple times, because, for example, diff --git a/core/logc/logs_test.go b/core/logc/logs_test.go index 3ee986423617..91a2d70041ca 100644 --- a/core/logc/logs_test.go +++ b/core/logc/logs_test.go @@ -132,6 +132,8 @@ func TestMisc(t *testing.T) { SetLevel(logx.DebugLevel) assert.NoError(t, SetUp(LogConf{})) assert.NoError(t, Close()) + l := GetLevel() + assert.Equal(t, logx.DebugLevel, l) } func TestSlow(t *testing.T) { diff --git a/core/logx/logs.go b/core/logx/logs.go index 2f84cd00fdd0..b4bc100d347a 100644 --- a/core/logx/logs.go +++ b/core/logx/logs.go @@ -268,6 +268,11 @@ func SetLevel(level uint32) { atomic.StoreUint32(&logLevel, level) } +// GetLevel returns the current log level. +func GetLevel() uint32 { + return atomic.LoadUint32(&logLevel) +} + // SetWriter sets the logging writer. It can be used to customize the logging. func SetWriter(w Writer) { if atomic.LoadUint32(&logLevel) != disableLevel { diff --git a/core/logx/logs_test.go b/core/logx/logs_test.go index c79c68da9579..4fe7592ae93e 100644 --- a/core/logx/logs_test.go +++ b/core/logx/logs_test.go @@ -558,6 +558,12 @@ func TestSetLevel(t *testing.T) { assert.Equal(t, 0, w.builder.Len()) } +func TestGetLevel(t *testing.T) { + SetLevel(ErrorLevel) + l := GetLevel() + assert.Equal(t, ErrorLevel, l) +} + func TestSetLevelTwiceWithMode(t *testing.T) { testModes := []string{ "console",