Skip to content

Commit d27a139

Browse files
committed
logUtil
1 parent b31c23f commit d27a139

File tree

6 files changed

+21
-9
lines changed

6 files changed

+21
-9
lines changed

app/Controller/IndexController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace App\Controller;
1414

15+
use App\Constants\ErrCode;
16+
use App\Exception\BusinessException;
1517
use App\JsonRpc\UserCenter\Contracts\UserCenterInterface;
1618
use App\Model\Model;
1719
use App\Model\User;
@@ -45,6 +47,7 @@ public function index()
4547
console()->emergency('emergency ----- Hello Hyperf!');
4648
console()->notice('notice ----- Hello Hyperf!');
4749
logger()->info('info ----- Hello Hyperf!');
50+
throw new BusinessException(ErrCode::SERVER_ERROR);
4851
return ResponseUtil::success(['count' => 1]);
4952
}
5053

app/Exception/Handler/AppExceptionHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function handle(Throwable $throwable, ResponseInterface $response)
3232
$this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()));
3333
$this->logger->error($throwable->getTraceAsString());
3434

35-
logger('error')->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()));
35+
logger()->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()));
3636

3737
return $response->withHeader('Server', 'hyperf')->withStatus(200)->withBody(new SwooleStream(ResponseUtil::fail(ErrCode::SERVER_ERROR)->getBody()->getContents()));
3838
}

app/Exception/Handler/AuthExceptionHandler.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace App\Exception\Handler;
55

66
use App\Constants\ErrCode;
7+
use App\Kernel\Log\LogUtil;
78
use Hyperf\HttpMessage\Stream\SwooleStream;
89
use Psr\Http\Message\ResponseInterface;
910
use Throwable;
@@ -13,10 +14,12 @@ class AuthExceptionHandler extends \Qbhy\HyperfAuth\AuthExceptionHandler
1314
public function handle(Throwable $throwable, ResponseInterface $response)
1415
{
1516
$this->stopPropagation();
16-
return $response->withStatus(200)->withBody(new SwooleStream(json_encode([
17+
$response = $response->withStatus(200)->withBody(new SwooleStream(json_encode([
1718
'code' => ErrCode::UNAUTHORIZED,
1819
'message' => $throwable->getMessage(),
1920
])));
21+
LogUtil::logResponse($response);
22+
return $response;
2023
}
2124

2225
}

app/Exception/Handler/BusinessExceptionHandler.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace App\Exception\Handler;
1414

1515
use App\Exception\BusinessException;
16+
use App\Kernel\Log\LogUtil;
1617
use Hyperf\ExceptionHandler\ExceptionHandler;
1718
use Hyperf\HttpMessage\Stream\SwooleStream;
1819
use Swow\Psr7\Message\ResponsePlusInterface;
@@ -23,10 +24,12 @@ class BusinessExceptionHandler extends ExceptionHandler
2324
public function handle(Throwable $throwable, ResponsePlusInterface $response)
2425
{
2526
$this->stopPropagation();
26-
return $response->withStatus(200)->withBody(new SwooleStream(json_encode([
27+
$response = $response->withStatus(200)->withBody(new SwooleStream(json_encode([
2728
'code' => $throwable->getCode(),
2829
'message' => $throwable->getMessage(),
2930
])));
31+
LogUtil::logResponse($response);
32+
return $response;
3033
}
3134

3235
public function isValid(Throwable $throwable): bool

app/Kernel/Log/LogUtil.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
use Hyperf\Codec\Json;
77
use Hyperf\Context\Context;
88
use Hyperf\HttpServer\Contract\RequestInterface;
9-
use Hyperf\HttpServer\Contract\ResponseInterface;
9+
use Psr\Http\Message\ResponseInterface;
1010
use Stringable;
1111
use function App\Kernel\console;
12+
use function App\Kernel\di;
1213
use function App\Kernel\logger;
1314

1415
/**
@@ -29,18 +30,18 @@ public static function __callStatic($name, $arguments)
2930
console()->$name(...$arguments);
3031
}
3132

32-
public static function logResponse()
33+
public static function logResponse(?Responseinterface $response = null)
3334
{
34-
$request = Context::get(RequestInterface::class);
35+
$request = di(RequestInterface::class);
3536
$requestId = Context::get('requestId');
36-
$response = Context::get(ResponseInterface::class);
37+
$response = $response?:di(\Hyperf\HttpServer\Contract\ResponseInterface::class);
3738
LogUtil::debug('request=' . Json::encode([
3839
'request_id' => $requestId,
3940
'uri' => $request->getUri()->getPath(),
4041
'method' => $request->getMethod(),
4142
'params' => $request->getParsedBody(),
4243
'query' => $request->getQueryParams(),
44+
'response' => $response->getBody()->getContents(),
4345
], JSON_UNESCAPED_SLASHES));
44-
LogUtil::debug('response=' . $response->getBody()->getContents());
4546
}
4647
}

app/Kernel/functions.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Psr\Log\LoggerInterface;
2121
use RuntimeException;
2222
use Throwable;
23+
use function Hyperf\Support\env;
2324

2425
/**
2526
* @template T
@@ -39,8 +40,9 @@ function di(?string $serviceName = null)
3940
}
4041
}
4142

42-
function logger($name = 'hyperf', $group = 'default'): LoggerInterface
43+
function logger($group = 'default'): LoggerInterface
4344
{
45+
$name = env('APP_NAME', 'hyperf');
4446
return di(LoggerFactory::class)->get($name, $group);
4547
}
4648

0 commit comments

Comments
 (0)