Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
de0ed058ca |
@@ -200,14 +200,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 +245,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 +253,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 +261,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 +269,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 +277,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 +289,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 +316,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
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,8 @@ print "FN: " . ml::fromName('Affe')->name . "<br>";
|
|||||||
print "NU: " . ml::fromValue(100)->name . "<br>";
|
print "NU: " . ml::fromValue(100)->name . "<br>";
|
||||||
print "NU: " . ml::fromValue(1000)->name . "<br>";
|
print "NU: " . ml::fromValue(1000)->name . "<br>";
|
||||||
|
|
||||||
$em->setErrorMsg('123', 'error', 'msg this is bad');
|
$em->setErrorMsg('123', 'error', 'msg this is bad, not logged');
|
||||||
|
$em->setErrorMsg('123', 'error', 'msg this is bad, logged', log_error:true);
|
||||||
$em->setErrorMsg('1000', 'info', 'This is good');
|
$em->setErrorMsg('1000', 'info', 'This is good');
|
||||||
$em->setErrorMsg('9999', 'abort', 'BAD: This is critical (abort)');
|
$em->setErrorMsg('9999', 'abort', 'BAD: This is critical (abort)');
|
||||||
$em->setErrorMsg('10-1000', 'wrong', 'Wrong level: This is emergency');
|
$em->setErrorMsg('10-1000', 'wrong', 'Wrong level: This is emergency');
|
||||||
|
|||||||
@@ -20,10 +20,21 @@ class ErrorMessage
|
|||||||
/** @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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -50,6 +61,8 @@ class ErrorMessage
|
|||||||
* highlight points for field highlights
|
* highlight points for field highlights
|
||||||
* @param string|null $message If abort/crash, non localized $str
|
* @param string|null $message If abort/crash, non localized $str
|
||||||
* @param array<mixed> $context Additionl info for abort/crash messages
|
* @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,
|
||||||
@@ -59,7 +72,11 @@ class ErrorMessage
|
|||||||
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
|
||||||
@@ -75,6 +92,14 @@ class ErrorMessage
|
|||||||
];
|
];
|
||||||
// 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,
|
||||||
@@ -109,6 +134,8 @@ class ErrorMessage
|
|||||||
* highlight points for field highlights
|
* highlight points for field highlights
|
||||||
* @param string|null $message If abort/crash, non localized $str
|
* @param string|null $message If abort/crash, non localized $str
|
||||||
* @param array<mixed> $context Additionl info for abort/crash messages
|
* @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,
|
||||||
@@ -118,8 +145,9 @@ class ErrorMessage
|
|||||||
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, $highlight, $message, $context, $log_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
// *********************************************************************
|
// *********************************************************************
|
||||||
@@ -162,6 +190,31 @@ class ErrorMessage
|
|||||||
'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__
|
||||||
|
|||||||
@@ -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__
|
||||||
|
|||||||
Reference in New Issue
Block a user