Refactor getRandomPrefix to simplify StoneLevel mapping.
All checks were successful
Go CI/CD / go-ci (push) Successful in 10m4s

Replaced the switch-case with a map for StoneLevel to prefix mapping. This reduces redundancy, improves readability, and maintains default handling for unsupported levels. Functionality remains unchanged.
This commit is contained in:
Rene Nochebuena 2025-04-09 23:04:35 -06:00
parent 94c6e882fb
commit 4046c447cc
Signed by: Rene Nochebuena
GPG Key ID: A9FD83117EA538D8

48
log.go
View File

@ -208,37 +208,27 @@ func logMessage(level StoneLevel, format string, args ...interface{}) {
}
}
// getRandomPrefix returns a random prefix based on the provided StoneLevel.
// It selects a prefix from predefined lists corresponding to the log level.
// getRandomPrefix returns a random prefix string based on the given StoneLevel.
// It selects from predefined lists corresponding to the severity of the level.
func getRandomPrefix(level StoneLevel) string {
switch level {
case TRACE:
idx := rand.Intn(len(stoneTracePrefixes))
return stoneTracePrefixes[idx]
case DEBUG:
idx := rand.Intn(len(stoneDebugPrefixes))
return stoneDebugPrefixes[idx]
case INFO:
idx := rand.Intn(len(stoneInfoPrefixes))
return stoneInfoPrefixes[idx]
case WARN:
idx := rand.Intn(len(stoneWarnPrefixes))
return stoneWarnPrefixes[idx]
case ERROR:
idx := rand.Intn(len(stoneErrorPrefixes))
return stoneErrorPrefixes[idx]
case FATAL:
idx := rand.Intn(len(stonePanicPrefixes))
return stonePanicPrefixes[idx]
default:
idx := rand.Intn(len(stoneInfoPrefixes))
return stoneInfoPrefixes[idx]
// Map StoneLevel to their respective prefix slices
prefixMap := map[StoneLevel][]string{
TRACE: stoneTracePrefixes,
DEBUG: stoneDebugPrefixes,
INFO: stoneInfoPrefixes,
WARN: stoneWarnPrefixes,
ERROR: stoneErrorPrefixes,
FATAL: stonePanicPrefixes,
}
// Default to INFO prefixes if level is not in the map
prefixes, exists := prefixMap[level]
if !exists {
prefixes = stoneInfoPrefixes
}
// Select and return a random prefix
return prefixes[rand.Intn(len(prefixes))]
}
// getRandomQuote returns a random character quote based on success or failure.