eee
This commit is contained in:
+30
-94
@@ -261,7 +261,8 @@ if (!function_exists('is_enable_file_modification_listening')) {
|
|||||||
/**
|
/**
|
||||||
* @return bool
|
* @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';
|
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')) {
|
if (!function_exists('event')) {
|
||||||
|
|
||||||
|
|
||||||
@@ -1044,7 +953,7 @@ if (!function_exists('throwable')) {
|
|||||||
if (is_string($throwable)) {
|
if (is_string($throwable)) {
|
||||||
return $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 .= ' File: ' . $throwable->getFile() . PHP_EOL;
|
||||||
$message .= ' Line: ' . $throwable->getLine() . 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')) {
|
if (!function_exists('map')) {
|
||||||
|
|
||||||
|
|||||||
@@ -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/');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,30 +68,31 @@ 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
|
||||||
|
* @param array $data
|
||||||
|
* @param mixed|null $result
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function dump($message): bool
|
public function json_log(Throwable $exception, array $data = [], mixed $result = null): mixed
|
||||||
{
|
{
|
||||||
$message = throwable($message);
|
json_log($exception, $data);
|
||||||
if (str_contains($message, 'inotify_rm_watch')) {
|
|
||||||
return false;
|
$this->println($exception->getMessage());
|
||||||
}
|
|
||||||
file_put_contents('php://output', '[' . date('Y-m-d H:i:s') . '] ' . $message, FILE_APPEND);
|
return $result;
|
||||||
$this->error($message, []);
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -118,8 +120,9 @@ 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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,6 +326,8 @@ class MongoDB
|
|||||||
|
|
||||||
return $client;
|
return $client;
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
|
\Kiri::getLogger()->json_log($e);
|
||||||
|
|
||||||
throw new RedisConnectException(sprintf('MongoDB Connect %s Fail: %s', $uri, $e->getMessage()));
|
throw new RedisConnectException(sprintf('MongoDB Connect %s Fail: %s', $uri, $e->getMessage()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user