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
+30 -94
View File
@@ -261,7 +261,8 @@ if (!function_exists('is_enable_file_modification_listening')) {
/**
* @return bool
*/
#[Pure] function is_enable_file_modification_listening(): bool
#[Pure]
function is_enable_file_modification_listening(): bool
{
return env('enable_file_modification_listening', 'off') == 'on';
}
@@ -923,98 +924,6 @@ if (!function_exists('merge')) {
}
if (!function_exists('jTraceEx')) {
/**
* @param $e
* @param null $seen
* @param bool $toHtml
* @return string
*/
function jTraceEx($e, $seen = NULL, bool $toHtml = FALSE): string
{
$starter = $seen ? 'Caused by: ' : '';
$result = [];
if (!$seen)
$seen = [];
$trace = $e->getTrace();
$prev = $e->getPrevious();
$result[] = sprintf('%s%s: %s', $starter, $e::class, $e->getMessage());
$file = $e->getFile();
$line = $e->getLine();
foreach ($trace as $value) {
$result[] = sprintf(' at %s%s%s(%s%s%s)',
count($value) && array_key_exists('class', $value) ? str_replace('\\', '.', $value['class']) : '',
count($value) && array_key_exists('class', $value) && array_key_exists('function', $value) ? '.' : '',
count($value) && array_key_exists('function', $value) ? str_replace('\\', '.', $value['function']) : '(main)',
$line === NULL ? $file : basename($file),
$line === NULL ? '' : ':',
$line === NULL ? '' : $line);
$file = array_key_exists('file', $value) ? $value['file'] : 'Unknown Source';
$line = array_key_exists('file', $value) && array_key_exists('line', $value) && $value['line'] ? $value['line'] : NULL;
}
$result = join($toHtml ? "<br>" : "\n", $result);
if ($prev) {
$result .= ($toHtml ? "<br>" : "\n") . jTraceEx($prev, $seen, $toHtml);
}
return $result;
}
}
if (!function_exists('error')) {
/**
* @param mixed $message
* @param array $method
* @return void
* @throws
*/
function error(mixed $message, array $method = []): void
{
if (is_string($message) && str_contains($message, 'inotify_rm_watch')) {
return;
}
Kiri::getLogger()->failure($message);
}
}
if (!function_exists('trigger_print_error')) {
/**
* @param mixed $message
* @param string $method
* @return bool
* @throws
*/
function trigger_print_error(mixed $message, string $method = 'app'): bool
{
return Kiri::getLogger()->failure($message, $method);
}
}
if (!function_exists('println')) {
/**
* @param mixed $message
* @param string $method
* @return bool
* @throws
*/
function println(mixed $message, string $method = 'app'): bool
{
return Kiri::getLogger()->failure($message, $method);
}
}
if (!function_exists('event')) {
@@ -1044,7 +953,7 @@ if (!function_exists('throwable')) {
if (is_string($throwable)) {
return $throwable;
}
$message = "\033[31m" . $throwable::class . ' ' . $throwable->getMessage() . "\033[0m" . PHP_EOL;
$message = $throwable::class . ' ' . $throwable->getMessage() . PHP_EOL;
$message .= ' File: ' . $throwable->getFile() . PHP_EOL;
$message .= ' Line: ' . $throwable->getLine() . PHP_EOL;
@@ -1080,6 +989,33 @@ if (!function_exists('throwable')) {
}
if (!function_exists('json_log')) {
function json_log(Throwable $throwable, array $data = []): void
{
$param = [];
$param['time'] = date('Y-m-d H:i:s');
$param['request'] = [
'path' => \request()->getUri()->getPath(),
'headers' => \request()->getHeaders(),
'method' => \request()->getMethod(),
'params' => \request()->getBody()->getContents(),
];
$param['exception'] = [
'message' => $throwable->getMessage(),
'code' => $throwable->getCode(),
'file' => $throwable->getFile(),
'line' => $throwable->getLine(),
'trace' => $throwable->getTrace(),
];
$param['data'] = $data;
file_put_contents(storage('exception-' . date('Y-m-d') . '.log','exception'),
json_encode($param, JSON_UNESCAPED_UNICODE),
FILE_APPEND);
}
}
if (!function_exists('map')) {
+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());
}
+16 -13
View File
@@ -9,6 +9,7 @@ use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use Throwable;
/**
@@ -67,30 +68,31 @@ 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
* @param Throwable $exception
* @param array $data
* @param mixed|null $result
* @return bool
*/
protected function dump($message): bool
public function json_log(Throwable $exception, array $data = [], mixed $result = null): mixed
{
$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;
json_log($exception, $data);
$this->println($exception->getMessage());
return $result;
}
@@ -118,8 +120,9 @@ 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);
}
}
+5 -1
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,6 +326,8 @@ class MongoDB
return $client;
} catch (\Throwable $e) {
\Kiri::getLogger()->json_log($e);
throw new RedisConnectException(sprintf('MongoDB Connect %s Fail: %s', $uri, $e->getMessage()));
}
}
+15 -6
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
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;
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