fix(logging): make level and field-name access concurrency-safe

SetFieldName wrote a package-global map with no lock while log read it
from many goroutines, and the level, caller, and time-format setters
mutated fields without synchronisation. A runtime setter call could
crash the process with a concurrent map write.

The scalar fields now use atomics on the hot log path and the global
field-name map uses a read-write mutex. Behaviour for init-time
configuration is unchanged.
This commit is contained in:
2026-08-14 11:10:01 +01:00
parent 159faa68e0
commit a774f9905b
3 changed files with 133 additions and 20 deletions
+3 -3
View File
@@ -100,7 +100,7 @@ func (suite *LoggerTestSuite) Test_LogsLevelsPrint() {
// Set logger's minimum log level
logger.SetMinLogLevel(tt.loggerMinLevel)
fmt.Println("Logger min log level:", levelNames[logger.minLogLevel])
fmt.Println("Logger min log level:", levelNames[logger.minLogLevel.Load()])
// Call the logging method
callLoggerMethod(logger, tt.method, msg)
@@ -138,13 +138,13 @@ func containsLogMessage(logOutput, expectedMessage string) bool {
func (suite *LoggerTestSuite) Test_SetFormat() {
logger := New().SetTimeFormat(time.RFC3339Nano)
assert.Equal(time.RFC3339Nano, logger.timeFormat)
assert.Equal(time.RFC3339Nano, *logger.timeFormat.Load())
}
func (suite *LoggerTestSuite) Test_SetMinLogLevel() {
logger := New().SetMinLogLevel(LEVEL_DEBUG)
assert.Equal(LEVEL_DEBUG, logger.minLogLevel)
assert.Equal(LEVEL_DEBUG, int(logger.minLogLevel.Load()))
}
func (suite *LoggerTestSuite) Test_ShouldLog() {