1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
declare(strict_types=1);
namespace Meibuyu\Micro\Exceptions\Handler;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\Logger\LoggerFactory;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Throwable;
class AppExceptionHandler extends ExceptionHandler
{
/**
* @var StdoutLoggerInterface
*/
protected $stdoutLogger;
/**
* @var LoggerInterface
*/
protected $logger;
public function __construct(StdoutLoggerInterface $stdoutLogger, LoggerFactory $loggerFactory)
{
$this->stdoutLogger = $stdoutLogger;
$this->logger = $loggerFactory->get('Uncaught Exception');
}
public function handle(Throwable $throwable, ResponseInterface $response)
{
// 捕获所有未捕获的异常
$this->stopPropagation();
$msg = sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile());
$this->stdoutLogger->error($msg);
$this->logger->error($msg);
$this->stdoutLogger->error($throwable->getTraceAsString());
return $response->withStatus(500)->withBody(new SwooleStream($msg));
}
public function isValid(Throwable $throwable): bool
{
return true;
}
}