CustomLogWriter.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Logging;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Log;
  5. use Spatie\HttpLogger\LogWriter;
  6. class CustomLogWriter implements LogWriter
  7. {
  8. public function logRequest(Request $request): void
  9. {
  10. $ip = $request->getClientIp();
  11. $method = strtoupper($request->getMethod());
  12. $uri = $request->getPathInfo();
  13. $bodyAsJson = json_encode($request->except(config('http-logger.except')), JSON_UNESCAPED_UNICODE);
  14. // 获取请求头信息
  15. $headers = json_encode($request->headers->all(), JSON_UNESCAPED_UNICODE);
  16. // $keyHeaders = [
  17. // // 'content-type',
  18. // // 'user-agent',
  19. // 'platform',
  20. // 'authorization'
  21. // ];
  22. // $headers = json_encode(array_intersect_key($request->headers->all(), array_flip($keyHeaders)));
  23. // // 在这里添加需要过滤的 URI
  24. $filteredUris = [
  25. '/app/rsaInteriorOrderSelect',
  26. '/app/getSelectPhoneInfo',
  27. ];
  28. if (in_array($uri, $filteredUris)) {
  29. return;
  30. }
  31. // 拼接日志信息
  32. $message = "{$ip} {$method} {$uri}" . PHP_EOL .
  33. "Headers: {$headers}" . PHP_EOL .
  34. "Body: {$bodyAsJson}";
  35. // 使用指定的日志通道记录信息
  36. Log::channel(config('http-logger.log_channel'))->info($message);
  37. }
  38. }