This commit is contained in:
2025-12-31 00:19:31 +08:00
parent 87e7535f27
commit baea90e9e5
6 changed files with 763 additions and 811 deletions
+714 -778
View File
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -72,10 +72,10 @@ class Application extends BaseApplication
{
if (!($beforeCommandExecute->command instanceof ServerCommand)) {
$scanner = $this->container->get(Scanner::class);
$scanner->load_directory(APP_PATH . 'app/');
$scanner->scan(APP_PATH . 'app/');
} else if (config('site.reload.hot', false) === false) {
$scanner = $this->container->get(Scanner::class);
$scanner->load_directory(APP_PATH . 'app/');
$scanner->scan(APP_PATH . 'app/');
}
}
+2 -2
View File
@@ -89,7 +89,7 @@ class ErrorHandler extends Component implements ErrorInterface
return;
}
$this->getLogger()->failure($lastError['message'] . PHP_EOL);
$this->getLogger()->logCategory($lastError['message'] . PHP_EOL);
event(new OnSystemError());
}
@@ -104,7 +104,7 @@ class ErrorHandler extends Component implements ErrorInterface
{
$this->category = 'exception';
$this->getLogger()->failure($exception);
$this->getLogger()->logCategory($exception);
event(new OnSystemError());
}
+22 -19
View File
@@ -9,6 +9,7 @@ use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use Throwable;
/**
@@ -67,31 +68,32 @@ class StdoutLogger extends Component
* @param string $model
* @return bool
*/
public function failure($message, string $model = 'app'): bool
public function logCategory($message, string $model = 'app'): bool
{
if ($message instanceof \Exception) {
$this->errors[$model] = $message->getMessage();
} else {
$this->errors[$model] = $message;
}
return $this->dump($message);
$this->println($message);
return false;
}
/**
* @param $message
* @return bool
*/
protected function dump($message): bool
{
$message = throwable($message);
if (str_contains($message, 'inotify_rm_watch')) {
return false;
}
file_put_contents('php://output', '[' . date('Y-m-d H:i:s') . '] ' . $message, FILE_APPEND);
$this->error($message, []);
return false;
}
/**
* @param Throwable $exception
* @param array $data
* @param mixed|null $result
* @return bool
*/
public function json_log(Throwable $exception, array $data = [], mixed $result = null): mixed
{
json_log($exception, $data);
$this->println($exception->getMessage());
return $result;
}
/**
@@ -118,9 +120,10 @@ class StdoutLogger extends Component
} else if (method_exists($this, $name)) {
$this->{$name}(...$arguments);
}
} catch (\Throwable $exception) {
file_put_contents('php://output', '[' . date('Y-m-d H:i:s') . '] ' . $exception->getMessage(), FILE_APPEND);
}
} catch (Throwable $exception) {
$this->println($exception->getMessage());
$this->json_log($exception);
}
}
+6 -2
View File
@@ -129,7 +129,9 @@ class MongoDB
throw new \BadMethodCallException("Method {$name} does not exist on MongoDB Client or Database.");
} catch (\Throwable $throwable) {
return trigger_print_error(throwable($throwable));
\Kiri::getLogger()->json_log($throwable);
return false;
} finally {
// MongoDB 连接是持久的,不需要释放
$this->pool()->push($this->getName(), $client);
@@ -324,7 +326,9 @@ class MongoDB
return $client;
} catch (\Throwable $e) {
throw new RedisConnectException(sprintf('MongoDB Connect %s Fail: %s', $uri, $e->getMessage()));
\Kiri::getLogger()->json_log($e);
throw new RedisConnectException(sprintf('MongoDB Connect %s Fail: %s', $uri, $e->getMessage()));
}
}
+17 -8
View File
@@ -103,13 +103,13 @@ class Redis
public function lock($key, int $timeout = 5): bool|int
{
$script = <<<SCRIPT
local _nx = redis.call('setnx',KEYS[1], ARGV[1])
if (_nx ~= 0) then
redis.call('expire',KEYS[1], ARGV[1])
return 1
end
return 0
SCRIPT;
local _nx = redis.call('setnx',KEYS[1], ARGV[1])
if (_nx ~= 0) then
redis.call('expire',KEYS[1], ARGV[1])
return 1
end
return 0
SCRIPT;
return $this->eval($script, ['{lock}:' . $key, $timeout], 1);
}
@@ -147,7 +147,7 @@ SCRIPT;
try {
return $client->{$name}(...$arguments);
} catch (\Throwable $throwable) {
return trigger_print_error(throwable($throwable));
return $this->getLogger()->json_log($throwable, [], false);
} finally {
if ($client->ping('h') == 'h') {
$this->pool()->push($this->getName(), $client);
@@ -156,6 +156,15 @@ SCRIPT;
}
/**
* @return Kiri\Error\StdoutLogger
*/
protected function getLogger(): Kiri\Error\StdoutLogger
{
return Kiri::getLogger();
}
/**
* @return \Redis
* @throws