Compare commits

...

3 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
Clemens Schwaighofer
86acbbb85b Per run logging was not set if flag changed
if setFlag was set for per_run or per_date the init values where not set

Fixed that on setFlag it is checked if we have per_date or per_run and
then set if not set.

Not that for date, set the flag will set a new date, for per run no.
2023-06-02 17:38:09 +09:00
Clemens Schwaighofer
8e0af7a5f7 Class var init fixes 2023-06-01 13:03:46 +09:00
5 changed files with 89 additions and 64 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

@@ -36,7 +36,7 @@ $log = new CoreLibs\Logging\Logging([
'log_per_date' => true, 'log_per_date' => true,
]); ]);
$PAGE_NAME = 'TEST CLASS: DEBUG LOGGING'; $PAGE_NAME = 'TEST CLASS: LOGGING';
print "<!DOCTYPE html>"; print "<!DOCTYPE html>";
print "<html><head><title>" . $PAGE_NAME . "</title><head>"; print "<html><head><title>" . $PAGE_NAME . "</title><head>";
print "<body>"; print "<body>";
@@ -44,12 +44,12 @@ print '<div><a href="class_test.php">Class Test Master</a></div>';
print '<div><h1>' . $PAGE_NAME . '</h1></div>'; print '<div><h1>' . $PAGE_NAME . '</h1></div>';
$log->logger2Debug(); $log->logger2Debug();
echo "<hr>";
print "Level 250: " . Level::fromValue(250)->getName() . "<br>"; print "Level 250: " . Level::fromValue(250)->getName() . "<br>";
print "Flag: per_run (from int): " . Flag::fromValue(2)->getName() . "<br>"; print "Flag: per_class (16) (from int): " . Flag::fromValue(16)->getName() . "<br>";
print "Flag: per_run getName(): " . Flag::per_class->getName() . "<br>"; print "Flag: per_class getName(): " . Flag::per_class->getName() . "<br>";
print "Flag: per_run ->name: " . Flag::per_class->name . "<br>"; print "Flag: per_class ->name: " . Flag::per_class->name . "<br>";
print "Flag: per_run ->value: " . Flag::per_class->value . "<br>"; print "Flag: per_class ->value: " . Flag::per_class->value . "<br>";
$log->setLogUniqueId(); $log->setLogUniqueId();
print "LogUniqId: " . $log->getLogUniqueId() . "<br>"; print "LogUniqId: " . $log->getLogUniqueId() . "<br>";
@@ -76,10 +76,19 @@ 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->debug('PER RUN', 'per run logging');
print "Log File: " . $log->getLogFile() . "<br>";
$log->unsetLogFlag(Flag::per_run);
// init empty // init empty
unset($LOG_FILE_ID); unset($LOG_FILE_ID);
$ll = new CoreLibs\Logging\Logging([ $ll = new CoreLibs\Logging\Logging([

View File

@@ -407,13 +407,6 @@ class Logging
} }
$this->setLogFlag($log_flag_key); $this->setLogFlag($log_flag_key);
} }
// init per run uid
if ($this->getLogFlag(Flag::per_run)) {
$this->setLogUniqueId();
} elseif ($this->getLogFlag(Flag::per_date)) {
// init file date
$this->log_file_date = date('Y-m-d');
}
} }
/** /**
@@ -535,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,
@@ -543,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);
@@ -562,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;
} }
@@ -776,6 +773,13 @@ class Logging
public function setLogFlag(Flag $flag): void public function setLogFlag(Flag $flag): void
{ {
$this->log_flags |= $flag->value; $this->log_flags |= $flag->value;
// init per run uid
if ($this->getLogFlag(Flag::per_run)) {
$this->setLogUniqueId();
} elseif ($this->getLogFlag(Flag::per_date)) {
// init file date
$this->setLogDate();
}
} }
/** /**
@@ -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,
) )

View File

@@ -232,13 +232,13 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
/** @var string */ /** @var string */
public string $col_name; // the name of the columen (before _<type>) [used for order button] public string $col_name; // the name of the columen (before _<type>) [used for order button]
/** @var int */ /** @var int */
public int $yes; // the yes flag that triggers the template to show ALL and not only new/load public int $yes = 0; // the yes flag that triggers the template to show ALL and not only new/load
/** @var string */ /** @var string */
public string $msg; // the error msg public string $msg = ''; // the error msg
/** @var int */ /** @var int */
public int $error; // the error flag set for printing red error msg public int $error = 0; // the error flag set for printing red error msg
/** @var int */ /** @var int */
public int $warning; // warning flag, for information (saved, loaded, etc) public int $warning = 0; // warning flag, for information (saved, loaded, etc)
/** @var string */ /** @var string */
public string $archive_pk_name; // the pk name for the load select form public string $archive_pk_name; // the pk name for the load select form
/** @var string */ /** @var string */
@@ -282,7 +282,7 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
public array $login_acl = []; public array $login_acl = [];
// layout publics // layout publics
/** @var int */ /** @var int */
public int $table_width; public int $table_width = 0;
// internal lang & encoding vars // internal lang & encoding vars
/** @var string */ /** @var string */
public string $lang_dir = ''; public string $lang_dir = '';

View File

@@ -77,79 +77,79 @@ class SmartyExtend extends \Smarty
public string $COMPILE_ID = ''; public string $COMPILE_ID = '';
// template vars // template vars
/** @var string */ /** @var string */
public string $MASTER_TEMPLATE_NAME; public string $MASTER_TEMPLATE_NAME = '';
/** @var string */ /** @var string */
public string $PAGE_FILE_NAME; public string $PAGE_FILE_NAME = '';
/** @var string */ /** @var string */
public string $CONTENT_INCLUDE; public string $CONTENT_INCLUDE = '';
/** @var string */ /** @var string */
public string $FORM_NAME; public string $FORM_NAME = '';
/** @var string */ /** @var string */
public string $FORM_ACTION; public string $FORM_ACTION = '';
/** @var string */ /** @var string */
public string $L_TITLE; public string $L_TITLE = '';
/** @var string|int */ /** @var string|int */
public string|int $PAGE_WIDTH; public string|int $PAGE_WIDTH;
// smarty include/set var // smarty include/set var
/** @var string */ /** @var string */
public string $TEMPLATE_PATH; public string $TEMPLATE_PATH = '';
/** @var string */ /** @var string */
public string $TEMPLATE_NAME; public string $TEMPLATE_NAME = '';
/** @var string */ /** @var string */
public string $INC_TEMPLATE_NAME; public string $INC_TEMPLATE_NAME = '';
/** @var string */ /** @var string */
public string $JS_TEMPLATE_NAME; public string $JS_TEMPLATE_NAME = '';
/** @var string */ /** @var string */
public string $CSS_TEMPLATE_NAME; public string $CSS_TEMPLATE_NAME = '';
/** @var string|null */ /** @var string|null */
public string|null $TEMPLATE_TRANSLATE; public string|null $TEMPLATE_TRANSLATE;
/** @var string|null */ /** @var string|null */
public string|null $JS_TRANSLATE; public string|null $JS_TRANSLATE;
// core group // core group
/** @var string */ /** @var string */
public string $JS_CORE_TEMPLATE_NAME; public string $JS_CORE_TEMPLATE_NAME = '';
/** @var string */ /** @var string */
public string $CSS_CORE_TEMPLATE_NAME; public string $CSS_CORE_TEMPLATE_NAME = '';
/** @var string */ /** @var string */
public string $JS_CORE_INCLUDE; public string $JS_CORE_INCLUDE = '';
/** @var string */ /** @var string */
public string $CSS_CORE_INCLUDE; public string $CSS_CORE_INCLUDE = '';
// local names // local names
/** @var string */ /** @var string */
public string $JS_SPECIAL_TEMPLATE_NAME = ''; public string $JS_SPECIAL_TEMPLATE_NAME = '';
/** @var string */ /** @var string */
public string $CSS_SPECIAL_TEMPLATE_NAME = ''; public string $CSS_SPECIAL_TEMPLATE_NAME = '';
/** @var string */ /** @var string */
public string $JS_INCLUDE; public string $JS_INCLUDE = '';
/** @var string */ /** @var string */
public string $CSS_INCLUDE; public string $CSS_INCLUDE = '';
/** @var string */ /** @var string */
public string $JS_SPECIAL_INCLUDE; public string $JS_SPECIAL_INCLUDE = '';
/** @var string */ /** @var string */
public string $CSS_SPECIAL_INCLUDE; public string $CSS_SPECIAL_INCLUDE = '';
/** @var string */ /** @var string */
public string $ADMIN_JAVASCRIPT; public string $ADMIN_JAVASCRIPT = '';
/** @var string */ /** @var string */
public string $ADMIN_STYLESHEET; public string $ADMIN_STYLESHEET = '';
/** @var string */ /** @var string */
public string $FRONTEND_JAVASCRIPT; public string $FRONTEND_JAVASCRIPT = '';
/** @var string */ /** @var string */
public string $FRONTEND_STYLESHEET; public string $FRONTEND_STYLESHEET = '';
// other smarty folder vars // other smarty folder vars
/** @var string */ /** @var string */
public string $INCLUDES; public string $INCLUDES = '';
/** @var string */ /** @var string */
public string $JAVASCRIPT; public string $JAVASCRIPT = '';
/** @var string */ /** @var string */
public string $CSS; public string $CSS = '';
/** @var string */ /** @var string */
public string $FONT; public string $FONT = '';
/** @var string */ /** @var string */
public string $PICTURES; public string $PICTURES = '';
/** @var string */ /** @var string */
public string $CACHE_PICTURES; public string $CACHE_PICTURES = '';
/** @var string */ /** @var string */
public string $CACHE_PICTURES_ROOT; public string $CACHE_PICTURES_ROOT = '';
// constructor class, just sets the language stuff // constructor class, just sets the language stuff
/** /**
@@ -373,7 +373,7 @@ class SmartyExtend extends \Smarty
// check for template include // check for template include
if ( if (
$this->USE_INCLUDE_TEMPLATE === true && $this->USE_INCLUDE_TEMPLATE === true &&
!$this->TEMPLATE_NAME empty($this->TEMPLATE_NAME)
) { ) {
$this->TEMPLATE_NAME = $this->CONTENT_INCLUDE; $this->TEMPLATE_NAME = $this->CONTENT_INCLUDE;
// add to cache & compile id // add to cache & compile id
@@ -390,7 +390,7 @@ class SmartyExtend extends \Smarty
exit('MASTER TEMPLATE: ' . $this->MASTER_TEMPLATE_NAME . ' could not be found'); exit('MASTER TEMPLATE: ' . $this->MASTER_TEMPLATE_NAME . ' could not be found');
} }
if ( if (
$this->TEMPLATE_NAME && !empty($this->TEMPLATE_NAME) &&
!file_exists($this->getTemplateDir()[0] . DIRECTORY_SEPARATOR . $this->TEMPLATE_NAME) !file_exists($this->getTemplateDir()[0] . DIRECTORY_SEPARATOR . $this->TEMPLATE_NAME)
) { ) {
exit('INCLUDE TEMPLATE: ' . $this->TEMPLATE_NAME . ' could not be found'); exit('INCLUDE TEMPLATE: ' . $this->TEMPLATE_NAME . ' could not be found');
@@ -418,7 +418,12 @@ class SmartyExtend extends \Smarty
} }
} }
// if we can't find it, dump it // if we can't find it, dump it
if (!file_exists($this->getTemplateDir()[0] . DIRECTORY_SEPARATOR . $this->TEMPLATE_TRANSLATE)) { if (
!file_exists(
$this->getTemplateDir()[0] . DIRECTORY_SEPARATOR
. $this->TEMPLATE_TRANSLATE
)
) {
$this->TEMPLATE_TRANSLATE = null; $this->TEMPLATE_TRANSLATE = null;
} }
if (empty($this->JS_TRANSLATE)) { if (empty($this->JS_TRANSLATE)) {