8000 feat : 增加异常打印至控制台 · APIJSON/hyperf-APIJSON@50a1421 · GitHub
[go: up one dir, main page]

Skip to content

Commit 50a1421

Browse files
committed
feat : 增加异常打印至控制台
1 parent b1cf719 commit 50a1421

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

app/Exception/Handler/AppExceptionHandler.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,29 @@
1212
namespace App\Exception\Handler;
1313

1414
use App\Constants\ResponseCode;
15+
use Hyperf\Contract\StdoutLoggerInterface;
1516
use Hyperf\ExceptionHandler\ExceptionHandler;
1617
use Hyperf\HttpMessage\Stream\SwooleStream;
1718
use Psr\Http\Message\ResponseInterface;
1819
use Throwable;
1920

2021
class AppExceptionHandler extends ExceptionHandler
2122
{
23+
24+
/**
25+
* @var StdoutLoggerInterface
26+
*/
27+
protected StdoutLoggerInterface $logger;
28+
29+
public function __construct(StdoutLoggerInterface $logger)
30+
{
31+
$this->logger = $logger;
32+
}
33+
2234
public function handle(Throwable $throwable, ResponseInterface $response)
2335
{
36+
$this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()));
37+
$this->logger->error($throwable->getTraceAsString());
2438
return $response->withHeader('Server', 'Hyperf')->withHeader('Content-Type', 'application/json')->withStatus(500)->withBody(new SwooleStream(json_encode([
2539
'code' => $throwable->getCode() ? $throwable->getCode() : ResponseCode::CODE_SERVER_ERROR,
2640
'msg' => $throwable->getCode() ? $throwable->getMessage() : ResponseCode::getMessage(ResponseCode::CODE_SERVER_ERROR)

0 commit comments

Comments
 (0)
0