From 00e64881853ea72b6a9de22790ffcd9240f102f7 Mon Sep 17 00:00:00 2001 From: Rene Nochebuena Date: Thu, 10 Apr 2025 09:50:54 -0600 Subject: [PATCH] Validate log level and improve test coverage 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. --- log.go | 10 +++++----- log_test.go | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/log.go b/log.go index 95efac2..c4516e2 100644 --- a/log.go +++ b/log.go @@ -111,6 +111,10 @@ func InitStoneLog(level StoneLevel, jsonMode, disableSuffixes bool) { // SetLogLevel sets the current log level to the specified StoneLevel. func SetLogLevel(level StoneLevel) { + if level < TRACE || level > PANIC { + return + } + currentLogLevel = level Observation("Log level set to %v", level) } @@ -221,11 +225,7 @@ func getRandomPrefix(level StoneLevel) string { FATAL: stonePanicPrefixes, } - // Default to INFO prefixes if level is not in the map - prefixes, exists := prefixMap[level] - if !exists { - prefixes = stoneInfoPrefixes - } + prefixes := prefixMap[level] // Select and return a random prefix return prefixes[rand.Intn(len(prefixes))] diff --git a/log_test.go b/log_test.go index 51c6a2d..64442fa 100644 --- a/log_test.go +++ b/log_test.go @@ -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) +}