This commit is contained in:
2023-04-18 23:47:31 +08:00
parent 88c2c430c6
commit af284dbe4b
6 changed files with 62 additions and 37 deletions
+24 -1
View File
@@ -1156,13 +1156,36 @@ if (!function_exists('error')) {
* @param mixed $message * @param mixed $message
* @param array $method * @param array $method
* @return void * @return void
* @throws ReflectionException
*/ */
function error(mixed $message, array $method = []): void function error(mixed $message, array $method = []): void
{ {
if ($message instanceof Throwable) {
$message = throwable($message);
}
Kiri::getLogger()->error($message, $method); Kiri::getLogger()->error($message, $method);
} }
} }
if (!function_exists('addError')) {
/**
* @param mixed $message
* @param string $method
* @return bool
* @throws ReflectionException
*/
function addError(mixed $message, string $method = 'app'): bool
{
$logger = Kiri::getLogger();
if ($message instanceof Throwable) {
$logger->error($message, [$message]);
}
return $logger->addError($message, $method);
}
}
if (!function_exists('success')) { if (!function_exists('success')) {
/** /**
@@ -1186,7 +1209,7 @@ if (!function_exists('throwable')) {
*/ */
function throwable(\Throwable|\Error $throwable): string function throwable(\Throwable|\Error $throwable): string
{ {
$message = "\033[31mError: " . $throwable->getMessage() . "\033[0m" . PHP_EOL . $message = "\033[31m" . $throwable->getMessage() . "\033[0m" . PHP_EOL .
' ' . $throwable->getFile() . " at line " . $throwable->getLine() . PHP_EOL; ' ' . $throwable->getFile() . " at line " . $throwable->getLine() . PHP_EOL;
foreach ($throwable->getTrace() as $value) { foreach ($throwable->getTrace() as $value) {
if (!isset($value['file'])) { if (!isset($value['file'])) {
+1 -1
View File
@@ -207,7 +207,7 @@ abstract class Actor implements ActorInterface, JsonSerializable
try { try {
$this->onUpdate(); $this->onUpdate();
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
\Kiri::getLogger()->error(throwable($exception)); error($exception);
} }
Coroutine::sleep($this->refreshInterval / 1000); Coroutine::sleep($this->refreshInterval / 1000);
+7 -4
View File
@@ -135,7 +135,7 @@ class Logger implements LoggerInterface
// TODO: Implement log() method. // TODO: Implement log() method.
$levels = Config::get('log.level', Logger::LOGGER_LEVELS); $levels = Config::get('log.level', Logger::LOGGER_LEVELS);
if (in_array($level, $levels)) { if (in_array($level, $levels)) {
$_string = '[' . now() . ']' . ucfirst($level) . ': ' . $message . PHP_EOL; $_string = "[" . now() . ']' . ucfirst($level) . ': ' . $message . PHP_EOL;
if (!empty($context)) { if (!empty($context)) {
$_string .= $this->_string($context); $_string .= $this->_string($context);
} }
@@ -189,11 +189,14 @@ class Logger implements LoggerInterface
private function _string($context): string private function _string($context): string
{ {
if ($context instanceof \Throwable) { if ($context instanceof \Throwable) {
$context = ['file' => $context->getFile(), 'line' => $context->getLine()]; $context = 'file -> ' . $context->getFile() . PHP_EOL . 'line -> ' . $context->getLine() . PHP_EOL;
} }
if (is_array($context) && isset($context[0]) && $context[0] instanceof \Throwable) { if (is_array($context) && isset($context[0]) && $context[0] instanceof \Throwable) {
$context = ['file' => $context[0]->getFile(), 'line' => $context[0]->getLine()]; $context = 'file -> ' . $context[0]->getFile() . PHP_EOL . 'line -> ' . $context[0]->getLine() . PHP_EOL;
} }
return print_r($context, true) . PHP_EOL; if (is_string($context)) {
return $context . PHP_EOL;
}
return implode(PHP_EOL, $context) . PHP_EOL;
} }
} }
-1
View File
@@ -23,7 +23,6 @@ class StdoutLogger extends Logger
*/ */
public function addError($message, string $model = 'app'): bool public function addError($message, string $model = 'app'): bool
{ {
$this->error($model, [$message]);
if ($message instanceof \Exception) { if ($message instanceof \Exception) {
$this->errors[$model] = $message->getMessage(); $this->errors[$model] = $message->getMessage();
} else { } else {
+1 -1
View File
@@ -172,7 +172,7 @@ class Connection extends Component
$result = true; $result = true;
} }
} catch (Error|Throwable $exception) { } catch (Error|Throwable $exception) {
$result = \Kiri::getLogger()->addError($exception, 'mysql'); $result = addError($exception, 'mysql');
} finally { } finally {
return $result; return $result;
} }
+1 -1
View File
@@ -186,7 +186,7 @@ SCRIPT;
try { try {
$response = $client->{$name}(...$arguments); $response = $client->{$name}(...$arguments);
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
$response = \Kiri::getLogger()->addError($throwable->getMessage()); $response = addError($throwable, 'redis');
} finally { } finally {
$pool = Kiri::getDi()->get(Pool::class); $pool = Kiri::getDi()->get(Pool::class);
$pool->push($this->host, $client); $pool->push($this->host, $client);