Compare commits

..

6 Commits

7 changed files with 258 additions and 35 deletions

View File

@@ -1 +1 @@
9.7.1 9.7.4

View File

@@ -35,6 +35,8 @@ class EditBase
private \CoreLibs\Output\Form\Generate $form; private \CoreLibs\Output\Form\Generate $form;
/** @var \CoreLibs\Logging\Logging */ /** @var \CoreLibs\Logging\Logging */
public \CoreLibs\Logging\Logging $log; public \CoreLibs\Logging\Logging $log;
/** @var \CoreLibs\Language\L10n */
public \CoreLibs\Language\L10n $l;
/** @var \CoreLibs\ACL\Login */ /** @var \CoreLibs\ACL\Login */
public \CoreLibs\ACL\Login $login; public \CoreLibs\ACL\Login $login;
@@ -57,6 +59,7 @@ class EditBase
) { ) {
$this->log = $log; $this->log = $log;
$this->login = $login; $this->login = $login;
$this->l = $l10n;
// smarty template engine (extended Translation version) // smarty template engine (extended Translation version)
$this->smarty = new \CoreLibs\Template\SmartyExtend( $this->smarty = new \CoreLibs\Template\SmartyExtend(
$l10n, $l10n,
@@ -77,7 +80,7 @@ class EditBase
echo "I am sorry, but this page cannot be viewed by a mobile phone"; echo "I am sorry, but this page cannot be viewed by a mobile phone";
exit; exit;
} }
// $this->form->log->debug('POST', $this->form->log->prAr($_POST)); // $this->log->debug('POST', $this->log->prAr($_POST));
} }
/** /**
@@ -179,7 +182,7 @@ class EditBase
} // while read data ... } // while read data ...
// html title // html title
$this->HEADER['HTML_TITLE'] = $this->form->l->__('Edit Order'); $this->HEADER['HTML_TITLE'] = $this->l->__('Edit Order');
$messages = []; $messages = [];
$error = $_POST['error'] ?? 0; $error = $_POST['error'] ?? 0;
@@ -632,7 +635,7 @@ class EditBase
'editAdmin_' . $this->smarty->lang 'editAdmin_' . $this->smarty->lang
); );
$this->form->log->debug('DEBUGEND', '==================================== [Form END]'); $this->log->debug('DEBUGEND', '==================================== [Form END]');
} }
} }

View File

@@ -15,15 +15,26 @@ use CoreLibs\Logging\Logger\MessageLevel;
class ErrorMessage class ErrorMessage
{ {
/** @var array<int,array{id:string,level:string,str:string,target:string,highlight:string[]}> */ /** @var array<int,array{id:string,level:string,str:string,target:string,target_style:string,highlight:string[]}> */
private array $error_str = []; private array $error_str = [];
/** @var \CoreLibs\Logging\Logging $log */ /** @var \CoreLibs\Logging\Logging $log */
public \CoreLibs\Logging\Logging $log; public \CoreLibs\Logging\Logging $log;
/** @var bool $log_error global flag to log error level message */
private bool $log_error = false;
/**
* init ErrorMessage
*
* @param \CoreLibs\Logging\Logging $log
* @param bool $log_error [=false]
*/
public function __construct( public function __construct(
\CoreLibs\Logging\Logging $log \CoreLibs\Logging\Logging $log,
bool $log_error = false
) { ) {
$this->log = $log; $this->log = $log;
$this->log_error = $log_error;
} }
/** /**
@@ -41,25 +52,36 @@ class ErrorMessage
* not set: unkown, will be logged as "emergency" * not set: unkown, will be logged as "emergency"
* target/highlight: id target name for frontend where to attach this message * target/highlight: id target name for frontend where to attach this message
* highlight is a list of other target points to highlight * highlight is a list of other target points to highlight
* for highlight targets css names are $level without a prefix and should be
* nested in the target element "input .error { ... }"
* target_style: if not set uses 'error-' $level as css style. applies to targets or main only
* *
* @param string $error_id Any internal error ID for this error * @param string $error_id Any internal error ID for this error
* @param string $level Error level in ok/info/warn/error * @param string $level Error level in ok/info/warn/error
* @param string $str Error message (out) * @param string $str Error message (out)
* @param string $target alternate attachment point for this error message * @param string $target alternate attachment point for this error message
* @param array<string> $highlight Any additional error data as error OR * @param string $target_style Alternate color style for the error message
* highlight points for field highlights * @param array<string> $highlight Any additional error data as error OR
* @param string|null $message If abort/crash, non localized $str * highlight points for field highlights
* @param array<mixed> $context Additionl info for abort/crash messages * @param string|null $message If abort/crash, non localized $str
* @param array<mixed> $context Additionl info for abort/crash messages
* @param bool|null $log_error [=null] log level 'error' to error, if null use global,
* else set for this call only
*/ */
public function setErrorMsg( public function setErrorMsg(
string $error_id, string $error_id,
string $level, string $level,
string $str, string $str,
string $target = '', string $target = '',
string $target_style = '',
array $highlight = [], array $highlight = [],
?string $message = null, ?string $message = null,
array $context = [], array $context = [],
?bool $log_error = null,
): void { ): void {
if ($log_error === null) {
$log_error = $this->log_error;
}
$original_level = $level; $original_level = $level;
$level = MessageLevel::fromName($level)->name; $level = MessageLevel::fromName($level)->name;
// if not string set, write message string if set, else level/error id // if not string set, write message string if set, else level/error id
@@ -71,10 +93,19 @@ class ErrorMessage
'level' => $level, 'level' => $level,
'str' => $str, 'str' => $str,
'target' => $target, 'target' => $target,
'target_style' => $target_style,
'highlight' => $highlight, 'highlight' => $highlight,
]; ];
// write to log for abort/crash // write to log for abort/crash
switch ($level) { switch ($level) {
case 'error':
if ($log_error) {
$this->log->error($message ?? $str, array_merge([
'id' => $error_id,
'level' => $original_level,
], $context));
}
break;
case 'abort': case 'abort':
$this->log->critical($message ?? $str, array_merge([ $this->log->critical($message ?? $str, array_merge([
'id' => $error_id, 'id' => $error_id,
@@ -101,25 +132,40 @@ class ErrorMessage
* Note, the parameter order is different and does not need an error id * Note, the parameter order is different and does not need an error id
* This is for backend alerts * This is for backend alerts
* *
* @param string $level error level (ok/warn/info/error) * @param string $level error level (ok/warn/info/error)
* @param string $str error string * @param string $str error string
* @param string|null $error_id optional error id for precise error lookup * @param string|null $error_id optional error id for precise error lookup
* @param string $target Alternate id name for output target on frontend * @param string $target Alternate id name for output target on frontend
* @param array<string> $highlight Any additional error data as error OR * @param string $target_style Alternate color style for the error message
* highlight points for field highlights * @param array<string> $highlight Any additional error data as error OR
* @param string|null $message If abort/crash, non localized $str * highlight points for field highlights
* @param array<mixed> $context Additionl info for abort/crash messages * @param string|null $message If abort/crash, non localized $str
* @param array<mixed> $context Additionl info for abort/crash messages
* @param bool|null $log_error [=null] log level 'error' to error, if null use global,
* else set for this call only
*/ */
public function setMessage( public function setMessage(
string $level, string $level,
string $str, string $str,
?string $error_id = null, ?string $error_id = null,
string $target = '', string $target = '',
string $target_style = '',
array $highlight = [], array $highlight = [],
?string $message = null, ?string $message = null,
array $context = [], array $context = [],
?bool $log_error = null,
): void { ): void {
$this->setErrorMsg($error_id ?? '', $level, $str, $target, $highlight, $message, $context); $this->setErrorMsg(
$error_id ?? '',
$level,
$str,
$target,
$target_style,
$highlight,
$message,
$context,
$log_error
);
} }
// ********************************************************************* // *********************************************************************
@@ -150,7 +196,7 @@ class ErrorMessage
* Gets the LAST entry in the array list. * Gets the LAST entry in the array list.
* If nothing found returns empty array set * If nothing found returns empty array set
* *
* @return array{id:string,level:string,str:string,target:string,highlight:string[]} Error block * @return array{id:string,level:string,str:string,target:string,target:string,highlight:string[]} Error block
*/ */
public function getLastErrorMsg(): array public function getLastErrorMsg(): array
{ {
@@ -159,9 +205,35 @@ class ErrorMessage
'str' => '', 'str' => '',
'id' => '', 'id' => '',
'target' => '', 'target' => '',
'target_string' => '',
'highlight' => [], 'highlight' => [],
]; ];
} }
// *********************************************************************
// FLAG SETTERS
// *********************************************************************
/**
* Set the log error flag
*
* @param bool $flag True to log level error too, False for do not (Default)
* @return void
*/
public function setFlagLogError(bool $flag): void
{
$this->log_error = $flag;
}
/**
* Get the current log error flag
*
* @return bool
*/
public function getFlagLogError(): bool
{
return $this->log_error;
}
} }
// __END__ // __END__

View File

@@ -46,6 +46,39 @@ enum MessageLevel: int
{ {
return self::tryFrom($value) ?? self::unknown; return self::tryFrom($value) ?? self::unknown;
} }
/**
* Returns true if the passed $level is higher or equal to $this
*
* @param MessageLevel $level
* @return bool
*/
public function includes(MessageLevel $level): bool
{
return $this->value <= $level->value;
}
/**
* If level is higher than set one
*
* @param MessageLevel $level
* @return bool
*/
public function isHigherThan(MessageLevel $level): bool
{
return $this->value > $level->value;
}
/**
* if level is lower than set one
*
* @param MessageLevel $level
* @return bool
*/
public function isLowerThan(MessageLevel $level): bool
{
return $this->value < $level->value;
}
} }
// __END__ // __END__

View File

@@ -300,7 +300,8 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
// log // log
/** @var \CoreLibs\Logging\Logging */ /** @var \CoreLibs\Logging\Logging */
public \CoreLibs\Logging\Logging $log; public \CoreLibs\Logging\Logging $log;
/** @var \CoreLibs\DB\Extended\ArrayIO */
public \CoreLibs\DB\Extended\ArrayIO $dba;
// now some default error msgs (english) // now some default error msgs (english)
/** @var array<mixed> */ /** @var array<mixed> */
public array $language_array = []; public array $language_array = [];
@@ -382,6 +383,15 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
$this->base_acl_level, $this->base_acl_level,
$this->acl_admin $this->acl_admin
); );
$this->dba = new \CoreLibs\DB\Extended\ArrayIO(
$db_config,
$config_array['table_array'],
$config_array['table_name'],
$this->log,
// set the ACL
$this->base_acl_level,
$this->acl_admin
);
// here should be a check if the config_array is correct ... // here should be a check if the config_array is correct ...
if (isset($config_array['show_fields']) && is_array($config_array['show_fields'])) { if (isset($config_array['show_fields']) && is_array($config_array['show_fields'])) {
$this->field_array = $config_array['show_fields']; $this->field_array = $config_array['show_fields'];

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace CoreLibs\Output\Form\TableArrays;
class EditOrder implements Interface\TableArraysInterface
{
/** @var \CoreLibs\Output\Form\Generate */
private \CoreLibs\Output\Form\Generate $form;
/**
* constructor
* @param \CoreLibs\Output\Form\Generate $form base form class
*/
public function __construct(\CoreLibs\Output\Form\Generate $form)
{
$this->form = $form;
$this->form->log->debug('CLASS LOAD', __NAMESPACE__ . __CLASS__);
}
/**
* NOTE: this is a dummy array to just init the Form\Generate class and is not used for anything else
*
* @return array<mixed>
*/
public function setTableArray(): array
{
return [
'table_array' => [
'-'
],
'table_name' => '-',
'load_query' => '',
'show_fields' => [],
];
}
}
// __END__

View File

@@ -106,6 +106,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'str' => $str, 'str' => $str,
'id' => '', 'id' => '',
'target' => '', 'target' => '',
'target_style' => '',
'highlight' => [], 'highlight' => [],
], ],
$em->getLastErrorMsg() $em->getLastErrorMsg()
@@ -139,6 +140,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'level' => 'info', 'level' => 'info',
'str' => 'INFO MESSAGE', 'str' => 'INFO MESSAGE',
'target' => '', 'target' => '',
'target_style' => '',
'highlight' => [], 'highlight' => [],
], ],
$em->getLastErrorMsg() $em->getLastErrorMsg()
@@ -154,6 +156,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'level' => 'info', 'level' => 'info',
'str' => 'INFO MESSAGE', 'str' => 'INFO MESSAGE',
'target' => '', 'target' => '',
'target_style' => '',
'highlight' => [], 'highlight' => [],
] ]
], ],
@@ -171,6 +174,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'level' => 'error', 'level' => 'error',
'str' => 'ERROR MESSAGE', 'str' => 'ERROR MESSAGE',
'target' => '', 'target' => '',
'target_style' => '',
'highlight' => [], 'highlight' => [],
], ],
$em->getLastErrorMsg() $em->getLastErrorMsg()
@@ -186,6 +190,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'level' => 'info', 'level' => 'info',
'str' => 'INFO MESSAGE', 'str' => 'INFO MESSAGE',
'target' => '', 'target' => '',
'target_style' => '',
'highlight' => [], 'highlight' => [],
], ],
[ [
@@ -193,6 +198,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'level' => 'error', 'level' => 'error',
'str' => 'ERROR MESSAGE', 'str' => 'ERROR MESSAGE',
'target' => '', 'target' => '',
'target_style' => '',
'highlight' => [], 'highlight' => [],
] ]
], ],
@@ -200,14 +206,44 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
); );
} }
/**
* Undocumented function
*
* @return array
*/
public function providerErrorMessageLog(): array public function providerErrorMessageLog(): array
{ {
return [ return [
'error, not logged' => [
'id' => '200',
'level' => 'error',
'str' => 'ERROR MESSAGE',
'message' => null,
'log_error' => null,
'expected' => '<ERROR> ERROR MESSAGE',
],
'error, logged' => [
'id' => '200',
'level' => 'error',
'str' => 'ERROR MESSAGE',
'message' => null,
'log_error' => true,
'expected' => '<ERROR> ERROR MESSAGE',
],
'error, logged, message' => [
'id' => '200',
'level' => 'error',
'str' => 'ERROR MESSAGE',
'message' => 'OTHER ERROR MESSAGE',
'log_error' => true,
'expected' => '<ERROR> OTHER ERROR MESSAGE',
],
'crash' => [ 'crash' => [
'id' => '300', 'id' => '300',
'level' => 'crash', 'level' => 'crash',
'str' => 'CRASH MESSAGE', 'str' => 'CRASH MESSAGE',
'message' => null, 'message' => null,
'log_error' => null,
'expected' => '<ALERT> CRASH MESSAGE', 'expected' => '<ALERT> CRASH MESSAGE',
], ],
'crash, message' => [ 'crash, message' => [
@@ -215,6 +251,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'level' => 'crash', 'level' => 'crash',
'str' => 'CRASH MESSAGE', 'str' => 'CRASH MESSAGE',
'message' => 'OTHER CRASH MESSAGE', 'message' => 'OTHER CRASH MESSAGE',
'log_error' => null,
'expected' => '<ALERT> OTHER CRASH MESSAGE', 'expected' => '<ALERT> OTHER CRASH MESSAGE',
], ],
'abort' => [ 'abort' => [
@@ -222,6 +259,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'level' => 'abort', 'level' => 'abort',
'str' => 'ABORT MESSAGE', 'str' => 'ABORT MESSAGE',
'message' => null, 'message' => null,
'log_error' => null,
'expected' => '<CRITICAL> ABORT MESSAGE', 'expected' => '<CRITICAL> ABORT MESSAGE',
], ],
'abort, message' => [ 'abort, message' => [
@@ -229,6 +267,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'level' => 'abort', 'level' => 'abort',
'str' => 'ABORT MESSAGE', 'str' => 'ABORT MESSAGE',
'message' => 'OTHER ABORT MESSAGE', 'message' => 'OTHER ABORT MESSAGE',
'log_error' => null,
'expected' => '<CRITICAL> OTHER ABORT MESSAGE', 'expected' => '<CRITICAL> OTHER ABORT MESSAGE',
], ],
'unknown' => [ 'unknown' => [
@@ -236,6 +275,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'level' => 'wrong level', 'level' => 'wrong level',
'str' => 'WRONG LEVEL MESSAGE', 'str' => 'WRONG LEVEL MESSAGE',
'message' => null, 'message' => null,
'log_error' => null,
'expected' => '<EMERGENCY> WRONG LEVEL MESSAGE', 'expected' => '<EMERGENCY> WRONG LEVEL MESSAGE',
], ],
'unknown, message' => [ 'unknown, message' => [
@@ -243,6 +283,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
'level' => 'wrong level', 'level' => 'wrong level',
'str' => 'WRONG LEVEL MESSAGE', 'str' => 'WRONG LEVEL MESSAGE',
'message' => 'OTHER WRONG LEVEL MESSAGE', 'message' => 'OTHER WRONG LEVEL MESSAGE',
'log_error' => null,
'expected' => '<EMERGENCY> OTHER WRONG LEVEL MESSAGE', 'expected' => '<EMERGENCY> OTHER WRONG LEVEL MESSAGE',
], ],
]; ];
@@ -254,10 +295,22 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
* @dataProvider providerErrorMessageLog * @dataProvider providerErrorMessageLog
* @testdox Test Log writing [$_dataName] * @testdox Test Log writing [$_dataName]
* *
* @param string $id
* @param string $level
* @param string $str
* @param string|null $message
* @param bool|null $log_error
* @param string $expected
* @return void * @return void
*/ */
public function testErrorMessageLog(string $id, string $level, string $str, ?string $message, string $expected) public function testErrorMessageLog(
{ string $id,
string $level,
string $str,
?string $message,
?bool $log_error,
string $expected
): void {
$log = new \CoreLibs\Logging\Logging([ $log = new \CoreLibs\Logging\Logging([
'log_file_id' => 'testErrorMessages', 'log_file_id' => 'testErrorMessages',
'log_folder' => self::LOG_FOLDER, 'log_folder' => self::LOG_FOLDER,
@@ -269,15 +322,27 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
$id, $id,
$level, $level,
$str, $str,
message: $message message: $message,
); log_error: $log_error
$file_content = file_get_contents(
$log->getLogFolder() . $log->getLogFile()
) ?: '';
$this->assertStringContainsString(
$expected,
$file_content
); );
$file_content = '';
if (is_file($log->getLogFolder() . $log->getLogFile())) {
$file_content = file_get_contents(
$log->getLogFolder() . $log->getLogFile()
) ?: '';
}
// if n
if ($level == 'error' && ($log_error === null || $log_error === false)) {
$this->assertStringNotContainsString(
$expected,
$file_content
);
} else {
$this->assertStringContainsString(
$expected,
$file_content
);
}
} }
} }