From 6dfb68a6da1ab19d23db1f1ca90d4311f6661b13 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Mon, 5 Jun 2023 09:34:29 +0900 Subject: [PATCH] Logging: internal fixes --- src/Logging/Logging.php | 28 +++++++++++-------- .../Logging/CoreLibsLoggingLoggingTest.php | 7 +++++ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/Logging/Logging.php b/src/Logging/Logging.php index 44e4d3b..ad9feed 100644 --- a/src/Logging/Logging.php +++ b/src/Logging/Logging.php @@ -528,7 +528,7 @@ class Logging * Prepare the log message with all needed info blocks: * [timestamp] [host name] [file path + file] [running uid] {class} - message * - * @param string $level_str Log level we will write to + * @param Level $level Log level we will write to * @param string|Stringable $message The message to write * @param mixed[] $context Any additional info we want to attach in any format * @param string $group_id A group id, only used in DEBUG level, @@ -536,11 +536,15 @@ class Logging * @return string */ private function prepareLog( - string $level_str, + Level $level, string|\Stringable $message, array $context = [], string $group_id = '', ): string { + // only prepare if to write log level is in set log level + if (!$this->checkLogLevel($level)) { + return ''; + } // file + line: call not this but one before (the one that calls this) $file_line = Support::getCallerFileLine(2) ?? System::getPageName(System::FULL_PATH); @@ -555,7 +559,7 @@ class Logging $timestamp = Support::printTime(); // if group id is empty replace it with current level - $group_str = $level_str; + $group_str = $level->getName(); if (!empty($group_id)) { $group_str .= ':' . $group_id; } @@ -927,9 +931,9 @@ class Logging array $context = [] ): bool { return $this->writeErrorMsg( - $this->log_level, + Level::Debug, $this->prepareLog( - Level::Debug->getName(), + Level::Debug, $prefix . $message, $context, $group_id @@ -950,7 +954,7 @@ class Logging return $this->writeErrorMsg( Level::Info, $this->prepareLog( - Level::Info->getName(), + Level::Info, $message, $context, ) @@ -969,7 +973,7 @@ class Logging return $this->writeErrorMsg( Level::Notice, $this->prepareLog( - Level::Notice->getName(), + Level::Notice, $message, $context, ) @@ -988,7 +992,7 @@ class Logging return $this->writeErrorMsg( Level::Warning, $this->prepareLog( - Level::Warning->getName(), + Level::Warning, $message, $context, ) @@ -1007,7 +1011,7 @@ class Logging return $this->writeErrorMsg( Level::Error, $this->prepareLog( - Level::Error->getName(), + Level::Error, $message, $context, ) @@ -1026,7 +1030,7 @@ class Logging return $this->writeErrorMsg( Level::Critical, $this->prepareLog( - Level::Critical->getName(), + Level::Critical, $message, $context, ) @@ -1045,7 +1049,7 @@ class Logging return $this->writeErrorMsg( Level::Alert, $this->prepareLog( - Level::Alert->getName(), + Level::Alert, $message, $context, ) @@ -1064,7 +1068,7 @@ class Logging return $this->writeErrorMsg( Level::Emergency, $this->prepareLog( - Level::Emergency->getName(), + Level::Emergency, $message, $context, ) diff --git a/test/phpunit/Logging/CoreLibsLoggingLoggingTest.php b/test/phpunit/Logging/CoreLibsLoggingLoggingTest.php index 1f637e1..f98e67d 100644 --- a/test/phpunit/Logging/CoreLibsLoggingLoggingTest.php +++ b/test/phpunit/Logging/CoreLibsLoggingLoggingTest.php @@ -796,6 +796,13 @@ final class CoreLibsLoggingLoggingTest extends TestCase ); } + // must test flow: + // init normal + // log -> check file name + // set per date + // log -> check file name + // and same for per_run + // deprecated calls check? }