diff --git a/config/packages/monolog.yaml b/config/packages/monolog.yaml index a376121..cd9e5b8 100644 --- a/config/packages/monolog.yaml +++ b/config/packages/monolog.yaml @@ -104,4 +104,4 @@ when@prod: type: stream channels: [deprecation] path: php://stderr - formatter: monolog.formatter.json + formatter: monolog.formatter.json \ No newline at end of file diff --git a/config/services.yaml b/config/services.yaml index 51f5ba0..83cc8eb 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -132,3 +132,8 @@ services: arguments: $httpClient: '@typo3.client' $apiKey: 'AbcAbc123'# dummy key, not yet implemented + + App\Logger\RequestIdProcessor: + tags: + - { name: monolog.processor } + diff --git a/src/BusProNet/ApiClient.php b/src/BusProNet/ApiClient.php index 761fac8..dd3b534 100644 --- a/src/BusProNet/ApiClient.php +++ b/src/BusProNet/ApiClient.php @@ -25,6 +25,7 @@ use App\Form\Model\RegistrationDto; use League\Flysystem\FilesystemException; use League\Flysystem\FilesystemOperator; use Psr\Log\LoggerInterface; +use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Serializer\Encoder\XmlEncoder; use Symfony\Component\Serializer\SerializerInterface; @@ -55,6 +56,7 @@ class ApiClient private readonly FilesystemOperator $xmlDump, private readonly LoggerInterface $logger, private readonly BookingDataProcessor $bookingDataProcessor, + private readonly RequestStack $requestStack, array $options, ) { $this->config = $this->resolveOptions($options); @@ -516,7 +518,7 @@ class ApiClient */ private function doSendRawXml(string $xml, bool $debug = false): string { - $requestId = date(DATE_ATOM).uniqid(); + $requestId = $this->getRequestId(); $doc = new \DOMDocument(); if (false === @$doc->loadXML($xml)) { @@ -631,7 +633,7 @@ class ApiClient */ private function doSendRequest(string $type, array $data, array $additionalArgs = [], bool $debug = false): mixed { - $requestId = date(DATE_ATOM).uniqid(); + $requestId = $this->getRequestId(); $body = $this ->serializer @@ -685,6 +687,12 @@ class ApiClient } } + private function getRequestId(): string + { + return $this->requestStack->getMainRequest()?->attributes->get('request_id') + ?? date(DATE_ATOM).uniqid(); + } + private function createKey(string $username, string $password, string $type): string { $date = (new \DateTimeImmutable())->format('Ymd'); diff --git a/src/EventListener/RequestIdListener.php b/src/EventListener/RequestIdListener.php new file mode 100644 index 0000000..036c373 --- /dev/null +++ b/src/EventListener/RequestIdListener.php @@ -0,0 +1,23 @@ +isMainRequest()) { + return; + } + + $requestId = date(DATE_ATOM).uniqid(); + $event->getRequest()->attributes->set('request_id', $requestId); + } +} diff --git a/src/Logger/RequestIdProcessor.php b/src/Logger/RequestIdProcessor.php new file mode 100644 index 0000000..4dbc082 --- /dev/null +++ b/src/Logger/RequestIdProcessor.php @@ -0,0 +1,28 @@ +requestStack->getMainRequest()?->attributes->get('request_id'); + + if (null !== $requestId) { + $record->extra['request_id'] = $requestId; + } + + return $record; + } +}