Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fe459aec80 | ||
|
|
de0ed058ca |
@@ -200,14 +200,44 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undocumented function
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerErrorMessageLog(): array
|
||||
{
|
||||
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' => [
|
||||
'id' => '300',
|
||||
'level' => 'crash',
|
||||
'str' => 'CRASH MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => null,
|
||||
'expected' => '<ALERT> CRASH MESSAGE',
|
||||
],
|
||||
'crash, message' => [
|
||||
@@ -215,6 +245,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
'level' => 'crash',
|
||||
'str' => 'CRASH MESSAGE',
|
||||
'message' => 'OTHER CRASH MESSAGE',
|
||||
'log_error' => null,
|
||||
'expected' => '<ALERT> OTHER CRASH MESSAGE',
|
||||
],
|
||||
'abort' => [
|
||||
@@ -222,6 +253,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
'level' => 'abort',
|
||||
'str' => 'ABORT MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => null,
|
||||
'expected' => '<CRITICAL> ABORT MESSAGE',
|
||||
],
|
||||
'abort, message' => [
|
||||
@@ -229,6 +261,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
'level' => 'abort',
|
||||
'str' => 'ABORT MESSAGE',
|
||||
'message' => 'OTHER ABORT MESSAGE',
|
||||
'log_error' => null,
|
||||
'expected' => '<CRITICAL> OTHER ABORT MESSAGE',
|
||||
],
|
||||
'unknown' => [
|
||||
@@ -236,6 +269,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
'level' => 'wrong level',
|
||||
'str' => 'WRONG LEVEL MESSAGE',
|
||||
'message' => null,
|
||||
'log_error' => null,
|
||||
'expected' => '<EMERGENCY> WRONG LEVEL MESSAGE',
|
||||
],
|
||||
'unknown, message' => [
|
||||
@@ -243,6 +277,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
'level' => 'wrong level',
|
||||
'str' => 'WRONG LEVEL MESSAGE',
|
||||
'message' => 'OTHER WRONG LEVEL MESSAGE',
|
||||
'log_error' => null,
|
||||
'expected' => '<EMERGENCY> OTHER WRONG LEVEL MESSAGE',
|
||||
],
|
||||
];
|
||||
@@ -254,10 +289,22 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
* @dataProvider providerErrorMessageLog
|
||||
* @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
|
||||
*/
|
||||
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_file_id' => 'testErrorMessages',
|
||||
'log_folder' => self::LOG_FOLDER,
|
||||
@@ -269,15 +316,27 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
|
||||
$id,
|
||||
$level,
|
||||
$str,
|
||||
message: $message
|
||||
);
|
||||
$file_content = file_get_contents(
|
||||
$log->getLogFolder() . $log->getLogFile()
|
||||
) ?: '';
|
||||
$this->assertStringContainsString(
|
||||
$expected,
|
||||
$file_content
|
||||
message: $message,
|
||||
log_error: $log_error
|
||||
);
|
||||
$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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
10
composer.lock
generated
10
composer.lock
generated
@@ -1180,16 +1180,16 @@
|
||||
},
|
||||
{
|
||||
"name": "phpstan/phpstan",
|
||||
"version": "1.10.33",
|
||||
"version": "1.10.34",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/phpstan/phpstan.git",
|
||||
"reference": "03b1cf9f814ba0863c4e9affea49a4d1ed9a2ed1"
|
||||
"reference": "7f806b6f1403e6914c778140e2ba07c293cb4901"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/03b1cf9f814ba0863c4e9affea49a4d1ed9a2ed1",
|
||||
"reference": "03b1cf9f814ba0863c4e9affea49a4d1ed9a2ed1",
|
||||
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/7f806b6f1403e6914c778140e2ba07c293cb4901",
|
||||
"reference": "7f806b6f1403e6914c778140e2ba07c293cb4901",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@@ -1238,7 +1238,7 @@
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2023-09-04T12:20:53+00:00"
|
||||
"time": "2023-09-13T09:49:47+00:00"
|
||||
},
|
||||
{
|
||||
"name": "phpstan/phpstan-deprecation-rules",
|
||||
|
||||
12
vendor/composer/installed.json
vendored
12
vendor/composer/installed.json
vendored
@@ -1179,17 +1179,17 @@
|
||||
},
|
||||
{
|
||||
"name": "phpstan/phpstan",
|
||||
"version": "1.10.33",
|
||||
"version_normalized": "1.10.33.0",
|
||||
"version": "1.10.34",
|
||||
"version_normalized": "1.10.34.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/phpstan/phpstan.git",
|
||||
"reference": "03b1cf9f814ba0863c4e9affea49a4d1ed9a2ed1"
|
||||
"reference": "7f806b6f1403e6914c778140e2ba07c293cb4901"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/03b1cf9f814ba0863c4e9affea49a4d1ed9a2ed1",
|
||||
"reference": "03b1cf9f814ba0863c4e9affea49a4d1ed9a2ed1",
|
||||
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/7f806b6f1403e6914c778140e2ba07c293cb4901",
|
||||
"reference": "7f806b6f1403e6914c778140e2ba07c293cb4901",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@@ -1198,7 +1198,7 @@
|
||||
"conflict": {
|
||||
"phpstan/phpstan-shim": "*"
|
||||
},
|
||||
"time": "2023-09-04T12:20:53+00:00",
|
||||
"time": "2023-09-13T09:49:47+00:00",
|
||||
"bin": [
|
||||
"phpstan",
|
||||
"phpstan.phar"
|
||||
|
||||
6
vendor/composer/installed.php
vendored
6
vendor/composer/installed.php
vendored
@@ -191,9 +191,9 @@
|
||||
'dev_requirement' => true,
|
||||
),
|
||||
'phpstan/phpstan' => array(
|
||||
'pretty_version' => '1.10.33',
|
||||
'version' => '1.10.33.0',
|
||||
'reference' => '03b1cf9f814ba0863c4e9affea49a4d1ed9a2ed1',
|
||||
'pretty_version' => '1.10.34',
|
||||
'version' => '1.10.34.0',
|
||||
'reference' => '7f806b6f1403e6914c778140e2ba07c293cb4901',
|
||||
'type' => 'library',
|
||||
'install_path' => __DIR__ . '/../phpstan/phpstan',
|
||||
'aliases' => array(),
|
||||
|
||||
BIN
vendor/phpstan/phpstan/phpstan.phar
vendored
BIN
vendor/phpstan/phpstan/phpstan.phar
vendored
Binary file not shown.
26
vendor/phpstan/phpstan/phpstan.phar.asc
vendored
26
vendor/phpstan/phpstan/phpstan.phar.asc
vendored
@@ -1,16 +1,16 @@
|
||||
-----BEGIN PGP SIGNATURE-----
|
||||
|
||||
iQIzBAABCgAdFiEEynwsejDI6OEnSoR2UcZzBf/C5cAFAmT1y40ACgkQUcZzBf/C
|
||||
5cDm6Q//VupQizxW3TICxhwzMDz05+nI8d72mn5vd94Niat90L1wPVHSZ9eBQSs3
|
||||
xMGVKJufB/3tQp6rviYk0aFnTFsKYkgwQYSDk1P9D7VrcJdpB461P3FT4MGsRk8E
|
||||
SSo55EuMGpiVvgoKbiPXeYV76yEpSMpelbKDNTm7iglLFVQKAnTv17aQv/UdBoYk
|
||||
t/dOS0ovoWmjkj9837EA5o2FAkhA5njlpvRs1l5Jt/tZsZtJTcDjPYfP5kIGbOwp
|
||||
V6xKYW7JYht+O0si3GxGmWGOE4VbOHLpUDpLMmVu53njjeGAIkPgX53TqBRbAbVL
|
||||
FjpDSZYC08lE4WblqXZEP6aKqVqthQ4LcGfyZDvuWDgPGHf86cTvfHQvtqRY45kU
|
||||
GhFDz6IfVlwBrfpc4hKfbhF6Q3Txm8ZSSfiVHim8JFjCqO+jn4qmaXGaOqbNUfv9
|
||||
fLgg7Bbnu1tZdMv6JJP5Kmk0x5wp4m2vFxDd6NGfm7uz9182/LtXOjqQ0MuGfL+j
|
||||
Dw53dl+WdUpr4Xk1m1GkYwDF9zLIwBB7dEghz6c4nbiGvh31MUk6N+4N2eGkjtWW
|
||||
B1PPORcpxxXSlhtftCwsJVxRFodQu9cmI0qmw4EUEipFrkFuT7a1rNF48NFyfRMh
|
||||
PcB/UcUHMCL7owhfQEEuPvqIPJS4saesCdFeJAM0MKuXB1nNGTQ=
|
||||
=HpaR
|
||||
iQIzBAABCgAdFiEEynwsejDI6OEnSoR2UcZzBf/C5cAFAmUBhZwACgkQUcZzBf/C
|
||||
5cDAIA/+JOxOdWCg6b1+/Bs4Opm7cmILJzh3Rw1366MzXFHJhe2n7exemzTE3X2E
|
||||
0HTjVVWoE0MpTt6knBgb+dulXTGm+yzJNIRLnCJFhsSdwUcxNAZMPozliZszGMcC
|
||||
HhGC1Ya7RU5d9WEg/lCAKRjNgSeY8VTRRfa49m6TQPQwGinXBNpH6q665feziUX5
|
||||
NB+rBMaWUDKp6GelPVrezEfME+SMfT66hW31n5m5paTwT+khk2Cenub5xXBUyXtz
|
||||
saxSayOShlUPN8kZKOjS/L/FPnOApGwcKxkX2kMK0qDs8YQFYHibNgnyhrEOBRD1
|
||||
UV9kBiU1+LUuxDcya8f0qo/gk6QN7Y/ywnIY3kTkqTYchCAAO+osu1amLYlInBSu
|
||||
r0LtQQ8+/llQ/bP0XS6cC6247J1fxO9xYMMmKVBfJrul3Wi6ETRI5WIdEQTSc2Z+
|
||||
rE00JY2X/vxJijUyGZ88+QDX3z7vGyaQJZiXq1XyjmBYw6w8UlxhGZcGGDIbTsb7
|
||||
lACLR/t4hgtE69QXW+L4P2ngU0agk5XXojg/+r+CnCWD/TdKcvs1WTn4TyuMZOLa
|
||||
2XF3h9/H0XMfE5LPlNJmb4YRjYjcNvPQwmGI4dmUMCJuTpjHUnpFXfswu4xgsaYV
|
||||
xq756DVfrRyG9PyXFfJZ66P7jt5+wisrVsWh/X3nX69P0ruwwss=
|
||||
=aOCq
|
||||
-----END PGP SIGNATURE-----
|
||||
|
||||
@@ -39,7 +39,9 @@ print "FN: " . ml::fromName('Affe')->name . "<br>";
|
||||
print "NU: " . ml::fromValue(100)->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, not logged', 'target-id', 'other-style');
|
||||
$em->setErrorMsg('123', 'error', 'msg this is bad, logged', log_error:true);
|
||||
$em->setErrorMsg('1000', 'info', 'This is good');
|
||||
$em->setErrorMsg('9999', 'abort', 'BAD: This is critical (abort)');
|
||||
$em->setErrorMsg('10-1000', 'wrong', 'Wrong level: This is emergency');
|
||||
|
||||
@@ -15,15 +15,26 @@ use CoreLibs\Logging\Logger\MessageLevel;
|
||||
|
||||
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 = [];
|
||||
/** @var \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(
|
||||
\CoreLibs\Logging\Logging $log
|
||||
\CoreLibs\Logging\Logging $log,
|
||||
bool $log_error = false
|
||||
) {
|
||||
$this->log = $log;
|
||||
$this->log_error = $log_error;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -41,25 +52,36 @@ class ErrorMessage
|
||||
* not set: unkown, will be logged as "emergency"
|
||||
* target/highlight: id target name for frontend where to attach this message
|
||||
* 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 $level Error level in ok/info/warn/error
|
||||
* @param string $str Error message (out)
|
||||
* @param string $target alternate attachment point for this error message
|
||||
* @param array<string> $highlight Any additional error data as error OR
|
||||
* highlight points for field highlights
|
||||
* @param string|null $message If abort/crash, non localized $str
|
||||
* @param array<mixed> $context Additionl info for abort/crash messages
|
||||
* @param string $error_id Any internal error ID for this error
|
||||
* @param string $level Error level in ok/info/warn/error
|
||||
* @param string $str Error message (out)
|
||||
* @param string $target alternate attachment point for this error message
|
||||
* @param string $target_style Alternate color style for the error message
|
||||
* @param array<string> $highlight Any additional error data as error OR
|
||||
* highlight points for field highlights
|
||||
* @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(
|
||||
string $error_id,
|
||||
string $level,
|
||||
string $str,
|
||||
string $target = '',
|
||||
string $target_style = '',
|
||||
array $highlight = [],
|
||||
?string $message = null,
|
||||
array $context = [],
|
||||
?bool $log_error = null,
|
||||
): void {
|
||||
if ($log_error === null) {
|
||||
$log_error = $this->log_error;
|
||||
}
|
||||
$original_level = $level;
|
||||
$level = MessageLevel::fromName($level)->name;
|
||||
// if not string set, write message string if set, else level/error id
|
||||
@@ -71,10 +93,19 @@ class ErrorMessage
|
||||
'level' => $level,
|
||||
'str' => $str,
|
||||
'target' => $target,
|
||||
'target_style' => $target_style,
|
||||
'highlight' => $highlight,
|
||||
];
|
||||
// write to log for abort/crash
|
||||
switch ($level) {
|
||||
case 'error':
|
||||
if ($log_error) {
|
||||
$this->log->error($message ?? $str, array_merge([
|
||||
'id' => $error_id,
|
||||
'level' => $original_level,
|
||||
], $context));
|
||||
}
|
||||
break;
|
||||
case 'abort':
|
||||
$this->log->critical($message ?? $str, array_merge([
|
||||
'id' => $error_id,
|
||||
@@ -101,25 +132,40 @@ class ErrorMessage
|
||||
* Note, the parameter order is different and does not need an error id
|
||||
* This is for backend alerts
|
||||
*
|
||||
* @param string $level error level (ok/warn/info/error)
|
||||
* @param string $str error string
|
||||
* @param string|null $error_id optional error id for precise error lookup
|
||||
* @param string $target Alternate id name for output target on frontend
|
||||
* @param array<string> $highlight Any additional error data as error OR
|
||||
* highlight points for field highlights
|
||||
* @param string|null $message If abort/crash, non localized $str
|
||||
* @param array<mixed> $context Additionl info for abort/crash messages
|
||||
* @param string $level error level (ok/warn/info/error)
|
||||
* @param string $str error string
|
||||
* @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_style Alternate color style for the error message
|
||||
* @param array<string> $highlight Any additional error data as error OR
|
||||
* highlight points for field highlights
|
||||
* @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(
|
||||
string $level,
|
||||
string $str,
|
||||
?string $error_id = null,
|
||||
string $target = '',
|
||||
string $target_style = '',
|
||||
array $highlight = [],
|
||||
?string $message = null,
|
||||
array $context = [],
|
||||
?bool $log_error = null,
|
||||
): 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.
|
||||
* 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
|
||||
{
|
||||
@@ -159,9 +205,35 @@ class ErrorMessage
|
||||
'str' => '',
|
||||
'id' => '',
|
||||
'target' => '',
|
||||
'target_string' => '',
|
||||
'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__
|
||||
|
||||
@@ -46,6 +46,39 @@ enum MessageLevel: int
|
||||
{
|
||||
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__
|
||||
|
||||
Reference in New Issue
Block a user