From ec3ca787fa7ead5572bea227fda9a8b56cb84cfc Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Mon, 5 Jun 2023 09:30:26 +0900 Subject: [PATCH] Logging: prepare message only if log level is high enough Also some clean ups on internal method call parameters --- .../Logging/CoreLibsLoggingLoggingTest.php | 7 +++++ www/admin/class_test.logging.php | 5 ++++ www/lib/CoreLibs/Logging/Logging.php | 28 +++++++++++-------- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/4dev/tests/Logging/CoreLibsLoggingLoggingTest.php b/4dev/tests/Logging/CoreLibsLoggingLoggingTest.php index 1f637e17..f98e67d4 100644 --- a/4dev/tests/Logging/CoreLibsLoggingLoggingTest.php +++ b/4dev/tests/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? } diff --git a/www/admin/class_test.logging.php b/www/admin/class_test.logging.php index ac605abf..cfb22029 100644 --- a/www/admin/class_test.logging.php +++ b/www/admin/class_test.logging.php @@ -76,7 +76,12 @@ with and > and < EOM)); $log->info('Info message', ['info' => 'log']); +$log->notice('Notice message', ['notice' => 'log']); +$log->warning('Warning message', ['warning' => 'log']); $log->error('Cannot process data', ['error' => 'log']); +$log->critical('Critical message', ['critical' => 'log']); +$log->alert('Alert message', ['Alert' => 'log']); +$log->emergency('Emergency message', ['Emergency' => 'log']); print "Log File: " . $log->getLogFile() . "
"; $log->setLogFlag(Flag::per_run); diff --git a/www/lib/CoreLibs/Logging/Logging.php b/www/lib/CoreLibs/Logging/Logging.php index 44e4d3b6..ad9feed8 100644 --- a/www/lib/CoreLibs/Logging/Logging.php +++ b/www/lib/CoreLibs/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, )