Validate log level and improve test coverage
Some checks failed
Go CI/CD / go-ci (push) Has been cancelled

Added validation to ensure log levels are within valid bounds in `SetLogLevel`. Simplified prefix mapping logic by removing the fallback to INFO level. Expanded test coverage with new test cases for plain text and JSON log outputs.
This commit is contained in:
Rene Nochebuena 2025-04-10 09:50:54 -06:00
parent b66a17197e
commit 00e6488185
Signed by: Rene Nochebuena
GPG Key ID: A9FD83117EA538D8
2 changed files with 27 additions and 5 deletions

10
log.go
View File

@ -111,6 +111,10 @@ func InitStoneLog(level StoneLevel, jsonMode, disableSuffixes bool) {
// SetLogLevel sets the current log level to the specified StoneLevel. // SetLogLevel sets the current log level to the specified StoneLevel.
func SetLogLevel(level StoneLevel) { func SetLogLevel(level StoneLevel) {
if level < TRACE || level > PANIC {
return
}
currentLogLevel = level currentLogLevel = level
Observation("Log level set to %v", level) Observation("Log level set to %v", level)
} }
@ -221,11 +225,7 @@ func getRandomPrefix(level StoneLevel) string {
FATAL: stonePanicPrefixes, FATAL: stonePanicPrefixes,
} }
// Default to INFO prefixes if level is not in the map prefixes := prefixMap[level]
prefixes, exists := prefixMap[level]
if !exists {
prefixes = stoneInfoPrefixes
}
// Select and return a random prefix // Select and return a random prefix
return prefixes[rand.Intn(len(prefixes))] return prefixes[rand.Intn(len(prefixes))]

View File

@ -45,3 +45,25 @@ func Test_LogLevelColor(t *testing.T) {
} }
} }
} }
func Test_PlainTextLogs(t *testing.T) {
InitStoneLog(TRACE, false, false)
Trace("This is a trace log")
Debug("This is a debug log")
Observation("This is an observation log")
Hypothesis("This is a hypothesis log")
Failure("This is a failure log")
SetLogLevel(INFO)
}
func Test_JSONLogs(t *testing.T) {
InitStoneLog(TRACE, true, false)
Trace("This is a trace log")
Debug("This is a debug log")
Observation("This is an observation log")
Hypothesis("This is a hypothesis log")
Failure("This is a failure log")
SetLogLevel(INFO)
}