Commit 5123a1cb authored by 王源's avatar 王源 🎧

Merge branch 'feature/exception' into test

parents 8c454916 be9d19f9
...@@ -27,7 +27,8 @@ ...@@ -27,7 +27,8 @@
"hyperf/service-governance": "~1.1.0", "hyperf/service-governance": "~1.1.0",
"fzaninotto/faker": "^1.9", "fzaninotto/faker": "^1.9",
"phpoffice/phpspreadsheet": "^1.8", "phpoffice/phpspreadsheet": "^1.8",
"dimsav/unix-zipper": "1.*" "dimsav/unix-zipper": "1.*",
"hyperf/amqp": "~1.1.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
......
<?php
declare(strict_types=1);
namespace Meibuyu\Micro\Amqp\Producer;
use Hyperf\Amqp\Annotation\Producer;
use Hyperf\Amqp\Message\ProducerMessage;
use Meibuyu\Micro\Exceptions\HttpResponseException;
use Meibuyu\Micro\Model\Auth;
/**
* @Producer(exchange="micro", routingKey="exception-log")
*/
class ExceptionLogProducer extends ProducerMessage
{
public function __construct($data)
{
$trace = explode("\n", $data['trace']);
try {
$data['operator'] = Auth::user()['name'];
} catch (HttpResponseException $e) {
put_log('获取操作人失败; ' . $e->getMessage(), 'ExceptionLogProducer.log');
}
try {
exec("git blame -L {$data['line']},{$data['line']} {$data['file']}", $output);
if (!empty($output[0]) && is_string($output[0])) {
preg_match('/(?<=\()[^ \)]+/', $output[0], $matches);
if (!empty($matches[0])) {
$data['coder'] = $matches[0];
}
}
} catch (\Exception $e) {
put_log('获取编码人失败; ' . $e->getMessage(), 'ExceptionLogProducer.log');
}
$this->payload = $data;
}
}
...@@ -4,16 +4,26 @@ declare(strict_types=1); ...@@ -4,16 +4,26 @@ declare(strict_types=1);
namespace Meibuyu\Micro\Exceptions\Handler; namespace Meibuyu\Micro\Exceptions\Handler;
use Hyperf\Amqp\Producer;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\ExceptionHandler\ExceptionHandler; use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream; use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\Logger\LoggerFactory; use Hyperf\Logger\LoggerFactory;
use Meibuyu\Micro\Amqp\Producer\ExceptionLogProducer;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Throwable; use Throwable;
class AppExceptionHandler extends ExceptionHandler class AppExceptionHandler extends ExceptionHandler
{ {
/**
* @var ContainerInterface
*/
protected $container;
/** /**
* @var StdoutLoggerInterface * @var StdoutLoggerInterface
*/ */
...@@ -24,20 +34,48 @@ class AppExceptionHandler extends ExceptionHandler ...@@ -24,20 +34,48 @@ class AppExceptionHandler extends ExceptionHandler
*/ */
protected $logger; protected $logger;
public function __construct(StdoutLoggerInterface $stdoutLogger, LoggerFactory $loggerFactory) /**
* @var ConfigInterface
*/
protected $config;
public function __construct(ContainerInterface $container)
{ {
$this->stdoutLogger = $stdoutLogger; $this->container = $container;
$this->logger = $loggerFactory->get('Uncaught Exception'); $this->stdoutLogger = $container->get(StdoutLoggerInterface::class);
$this->logger = $container->get(LoggerFactory::class)->get('Uncaught Exception');
$this->config = $container->get(ConfigInterface::class);
} }
public function handle(Throwable $throwable, ResponseInterface $response) public function handle(Throwable $throwable, ResponseInterface $response)
{ {
// 捕获所有未捕获的异常 // 捕获所有未捕获的异常
$this->stopPropagation(); $this->stopPropagation();
$msg = sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()); $message = $throwable->getMessage();
$line = $throwable->getLine();
$file = $throwable->getFile();
$trace = $throwable->getTraceAsString();
$code = $throwable->getCode();
$data = [
'server' => $this->config->get('app_name'),
'file' => $file,
'line' => $line,
'message' => $message,
'trace' => $trace,
'code' => $code,
'created_at' => now(),
];
try {
$exceptionLogProducer = new ExceptionLogProducer($data);
$producer = $this->container->get(Producer::class);
$producer->produce($exceptionLogProducer);
} catch (\Exception $e) {
put_log('异常日志失败; ' . $e->getMessage(), 'ExceptionLogProducer.log');
}
$msg = sprintf('%s[%s] in %s', $message, $line, $file);
$this->stdoutLogger->error($msg); $this->stdoutLogger->error($msg);
$this->logger->error($msg); $this->logger->error($msg);
$this->stdoutLogger->error($throwable->getTraceAsString()); $this->stdoutLogger->error($trace);
return $response->withStatus(500)->withBody(new SwooleStream($msg)); return $response->withStatus(500)->withBody(new SwooleStream($msg));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment