Compare commits

..

1 Commits

Author SHA1 Message Date
Clemens Schwaighofer
ec3ca787fa Logging: prepare message only if log level is high enough
Also some clean ups on internal method call parameters
2023-06-05 09:30:26 +09:00
3 changed files with 28 additions and 12 deletions

View File

@@ -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? // deprecated calls check?
} }

View File

@@ -76,7 +76,12 @@ with
and > and < and > and <
EOM)); EOM));
$log->info('Info message', ['info' => 'log']); $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->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() . "<br>"; print "Log File: " . $log->getLogFile() . "<br>";
$log->setLogFlag(Flag::per_run); $log->setLogFlag(Flag::per_run);

View File

@@ -528,7 +528,7 @@ class Logging
* Prepare the log message with all needed info blocks: * Prepare the log message with all needed info blocks:
* [timestamp] [host name] [file path + file] [running uid] {class} <debug level/group id> - message * [timestamp] [host name] [file path + file] [running uid] {class} <debug level/group id> - 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 string|Stringable $message The message to write
* @param mixed[] $context Any additional info we want to attach in any format * @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, * @param string $group_id A group id, only used in DEBUG level,
@@ -536,11 +536,15 @@ class Logging
* @return string * @return string
*/ */
private function prepareLog( private function prepareLog(
string $level_str, Level $level,
string|\Stringable $message, string|\Stringable $message,
array $context = [], array $context = [],
string $group_id = '', string $group_id = '',
): string { ): 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: call not this but one before (the one that calls this)
$file_line = Support::getCallerFileLine(2) ?? $file_line = Support::getCallerFileLine(2) ??
System::getPageName(System::FULL_PATH); System::getPageName(System::FULL_PATH);
@@ -555,7 +559,7 @@ class Logging
$timestamp = Support::printTime(); $timestamp = Support::printTime();
// if group id is empty replace it with current level // if group id is empty replace it with current level
$group_str = $level_str; $group_str = $level->getName();
if (!empty($group_id)) { if (!empty($group_id)) {
$group_str .= ':' . $group_id; $group_str .= ':' . $group_id;
} }
@@ -927,9 +931,9 @@ class Logging
array $context = [] array $context = []
): bool { ): bool {
return $this->writeErrorMsg( return $this->writeErrorMsg(
$this->log_level, Level::Debug,
$this->prepareLog( $this->prepareLog(
Level::Debug->getName(), Level::Debug,
$prefix . $message, $prefix . $message,
$context, $context,
$group_id $group_id
@@ -950,7 +954,7 @@ class Logging
return $this->writeErrorMsg( return $this->writeErrorMsg(
Level::Info, Level::Info,
$this->prepareLog( $this->prepareLog(
Level::Info->getName(), Level::Info,
$message, $message,
$context, $context,
) )
@@ -969,7 +973,7 @@ class Logging
return $this->writeErrorMsg( return $this->writeErrorMsg(
Level::Notice, Level::Notice,
$this->prepareLog( $this->prepareLog(
Level::Notice->getName(), Level::Notice,
$message, $message,
$context, $context,
) )
@@ -988,7 +992,7 @@ class Logging
return $this->writeErrorMsg( return $this->writeErrorMsg(
Level::Warning, Level::Warning,
$this->prepareLog( $this->prepareLog(
Level::Warning->getName(), Level::Warning,
$message, $message,
$context, $context,
) )
@@ -1007,7 +1011,7 @@ class Logging
return $this->writeErrorMsg( return $this->writeErrorMsg(
Level::Error, Level::Error,
$this->prepareLog( $this->prepareLog(
Level::Error->getName(), Level::Error,
$message, $message,
$context, $context,
) )
@@ -1026,7 +1030,7 @@ class Logging
return $this->writeErrorMsg( return $this->writeErrorMsg(
Level::Critical, Level::Critical,
$this->prepareLog( $this->prepareLog(
Level::Critical->getName(), Level::Critical,
$message, $message,
$context, $context,
) )
@@ -1045,7 +1049,7 @@ class Logging
return $this->writeErrorMsg( return $this->writeErrorMsg(
Level::Alert, Level::Alert,
$this->prepareLog( $this->prepareLog(
Level::Alert->getName(), Level::Alert,
$message, $message,
$context, $context,
) )
@@ -1064,7 +1068,7 @@ class Logging
return $this->writeErrorMsg( return $this->writeErrorMsg(
Level::Emergency, Level::Emergency,
$this->prepareLog( $this->prepareLog(
Level::Emergency->getName(), Level::Emergency,
$message, $message,
$context, $context,
) )