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)) { if (!($beforeCommandExecute->command instanceof ServerCommand)) {
$scanner = $this->container->get(Scanner::class); $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) { } else if (config('site.reload.hot', false) === false) {
$scanner = $this->container->get(Scanner::class); $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; return;
} }
$this->getLogger()->failure($lastError['message'] . PHP_EOL); $this->getLogger()->logCategory($lastError['message'] . PHP_EOL);
event(new OnSystemError()); event(new OnSystemError());
} }
@@ -104,7 +104,7 @@ class ErrorHandler extends Component implements ErrorInterface
{ {
$this->category = 'exception'; $this->category = 'exception';
$this->getLogger()->failure($exception); $this->getLogger()->logCategory($exception);
event(new OnSystemError()); event(new OnSystemError());
} }
+22 -19
View File
@@ -9,6 +9,7 @@ use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler; use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger; use Monolog\Logger;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Throwable;
/** /**
@@ -67,31 +68,32 @@ class StdoutLogger extends Component
* @param string $model * @param string $model
* @return bool * @return bool
*/ */
public function failure($message, string $model = 'app'): bool public function logCategory($message, string $model = 'app'): bool
{ {
if ($message instanceof \Exception) { if ($message instanceof \Exception) {
$this->errors[$model] = $message->getMessage(); $this->errors[$model] = $message->getMessage();
} else { } else {
$this->errors[$model] = $message; $this->errors[$model] = $message;
} }
return $this->dump($message); $this->println($message);
return false;
} }
/** /**
* @param $message * @param Throwable $exception
* @return bool * @param array $data
*/ * @param mixed|null $result
protected function dump($message): bool * @return bool
{ */
$message = throwable($message); public function json_log(Throwable $exception, array $data = [], mixed $result = null): mixed
if (str_contains($message, 'inotify_rm_watch')) { {
return false; json_log($exception, $data);
}
file_put_contents('php://output', '[' . date('Y-m-d H:i:s') . '] ' . $message, FILE_APPEND); $this->println($exception->getMessage());
$this->error($message, []);
return false; return $result;
} }
/** /**
@@ -118,9 +120,10 @@ class StdoutLogger extends Component
} else if (method_exists($this, $name)) { } else if (method_exists($this, $name)) {
$this->{$name}(...$arguments); $this->{$name}(...$arguments);
} }
} catch (\Throwable $exception) { } catch (Throwable $exception) {
file_put_contents('php://output', '[' . date('Y-m-d H:i:s') . '] ' . $exception->getMessage(), FILE_APPEND); $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."); throw new \BadMethodCallException("Method {$name} does not exist on MongoDB Client or Database.");
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
return trigger_print_error(throwable($throwable)); \Kiri::getLogger()->json_log($throwable);
return false;
} finally { } finally {
// MongoDB 连接是持久的,不需要释放 // MongoDB 连接是持久的,不需要释放
$this->pool()->push($this->getName(), $client); $this->pool()->push($this->getName(), $client);
@@ -324,7 +326,9 @@ class MongoDB
return $client; return $client;
} catch (\Throwable $e) { } 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 public function lock($key, int $timeout = 5): bool|int
{ {
$script = <<<SCRIPT $script = <<<SCRIPT
local _nx = redis.call('setnx',KEYS[1], ARGV[1]) local _nx = redis.call('setnx',KEYS[1], ARGV[1])
if (_nx ~= 0) then if (_nx ~= 0) then
redis.call('expire',KEYS[1], ARGV[1]) redis.call('expire',KEYS[1], ARGV[1])
return 1 return 1
end end
return 0 return 0
SCRIPT; SCRIPT;
return $this->eval($script, ['{lock}:' . $key, $timeout], 1); return $this->eval($script, ['{lock}:' . $key, $timeout], 1);
} }
@@ -147,7 +147,7 @@ SCRIPT;
try { try {
return $client->{$name}(...$arguments); return $client->{$name}(...$arguments);
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
return trigger_print_error(throwable($throwable)); return $this->getLogger()->json_log($throwable, [], false);
} finally { } finally {
if ($client->ping('h') == 'h') { if ($client->ping('h') == 'h') {
$this->pool()->push($this->getName(), $client); $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 * @return \Redis
* @throws * @throws