diff --git a/4dev/tests/CoreLibsDebugLoggingTest.php b/4dev/tests/CoreLibsDebugLoggingTest.php
index 4aeab1d4..791efb21 100644
--- a/4dev/tests/CoreLibsDebugLoggingTest.php
+++ b/4dev/tests/CoreLibsDebugLoggingTest.php
@@ -6,6 +6,8 @@ namespace tests;
use PHPUnit\Framework\TestCase;
+// TODO: setLogPer test log file written matches pattern
+
/**
* Test class for Debug\Logging
* @coversDefaultClass \CoreLibs\Debug\Logging
diff --git a/www/admin/class_test.debug.php b/www/admin/class_test.debug.php
index d22ddf98..3b88d090 100644
--- a/www/admin/class_test.debug.php
+++ b/www/admin/class_test.debug.php
@@ -175,6 +175,12 @@ print "GETCALLERCLASS(NON CLASS): " . \CoreLibs\Debug\Support::getCallerClass()
print "S::FSETFILENAME: " . FileWriter::fsetFilename('class_test_debug_file.log') . "
";
print "S::FDEBUG: " . FileWriter::fdebug('CLASS TEST DEBUG FILE: ' . date('Y-m-d H:i:s')) . "
";
+// test per level
+$debug->setLogPer('level', true);
+$debug->debug('TEST PER LEVEL', 'Per level test');
+$debug->debug('()', 'Per level test: invalid chars');
+$debug->setLogPer('level', false);
+
// error message
// future DEPRECATED
// $debug->debug('BASIC CLASS', 'Debug test');
diff --git a/www/lib/CoreLibs/Debug/Logging.php b/www/lib/CoreLibs/Debug/Logging.php
index 93b5f356..06b57424 100644
--- a/www/lib/CoreLibs/Debug/Logging.php
+++ b/www/lib/CoreLibs/Debug/Logging.php
@@ -365,7 +365,18 @@ class Logging
}
$fn = str_replace('##DATE##', $rpl_string, $fn); // create output filename
- $rpl_string = !$this->log_per_level ? '' : '_' . $level; // if request to write to one file
+ // write per level
+ $rpl_string = !$this->log_per_level ? '' :
+ // normalize level, replace all non alphanumeric characters with -
+ '_' . (
+ // if return is only - then set error string
+ preg_match(
+ "/^-+$/",
+ $level_string = preg_replace("/[^A-Za-z0-9-_]/", '-', $level) ?? ''
+ ) ?
+ 'INVALID-LEVEL-STRING' :
+ $level_string
+ );
$fn = str_replace('##LEVEL##', $rpl_string, $fn); // create output filename
// set per class, but don't use get_class as we will only get self
$rpl_string = !$this->log_per_class ? '' : '_'