12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?php
- namespace App\Logging;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- use Spatie\HttpLogger\LogWriter;
- class CustomLogWriter implements LogWriter
- {
- public function logRequest(Request $request): void
- {
- $ip = $request->getClientIp();
- $method = strtoupper($request->getMethod());
- $uri = $request->getPathInfo();
- $bodyAsJson = json_encode($request->except(config('http-logger.except')), JSON_UNESCAPED_UNICODE);
- // 获取请求头信息
- $headers = json_encode($request->headers->all(), JSON_UNESCAPED_UNICODE);
- // $keyHeaders = [
- // // 'content-type',
- // // 'user-agent',
- // 'platform',
- // 'authorization'
- // ];
- // $headers = json_encode(array_intersect_key($request->headers->all(), array_flip($keyHeaders)));
- // // 在这里添加需要过滤的 URI
- $filteredUris = [
- '/app/rsaInteriorOrderSelect',
- '/app/getSelectPhoneInfo',
- ];
- if (in_array($uri, $filteredUris)) {
- return;
- }
- // 拼接日志信息
- $message = "{$ip} {$method} {$uri}" . PHP_EOL .
- "Headers: {$headers}" . PHP_EOL .
- "Body: {$bodyAsJson}";
- // 使用指定的日志通道记录信息
- Log::channel(config('http-logger.log_channel'))->info($message);
- }
- }
|