Skip to content

Commit

Permalink
Fix log extra data and add unique request id
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Feb 7, 2024
1 parent dba444e commit 2e7fefc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/EventListener/RequestStatsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
namespace App\EventListener;

use Graze\DogStatsD\Client;
use Monolog\LogRecord;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
Expand All @@ -23,6 +25,7 @@ class RequestStatsListener

public function __construct(
private Client $statsd,
private LoggerInterface $logger,
) {
}

Expand All @@ -33,6 +36,13 @@ public function onRequest(RequestEvent $e): void
return;
}
$this->pageTiming = microtime(true);

$reqId = bin2hex(random_bytes(6));
$this->logger->pushProcessor(static function (LogRecord $record) use ($reqId) {

Check failure on line 41 in src/EventListener/RequestStatsListener.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined method Psr\Log\LoggerInterface::pushProcessor().
$record->extra['req_id'] = $reqId;

return $record;
});
}

#[AsEventListener]
Expand All @@ -46,6 +56,7 @@ public function onResponse(ResponseEvent $e): void
return;
}

$this->logger->popProcessor();

Check failure on line 59 in src/EventListener/RequestStatsListener.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined method Psr\Log\LoggerInterface::popProcessor().
$this->statsd->timing('app.response_time', (int) ((microtime(true) - $this->pageTiming) * 1000));
$this->pageTiming = null;

Expand Down
2 changes: 1 addition & 1 deletion src/Service/QueueWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private function process(string $jobId, SignalHandler $signal): bool
}

$this->logger->pushProcessor(static function (LogRecord $record) use ($job) {
$record->extra['job-id'] = $job->getId();
$record->extra['job_id'] = $job->getId();

return $record;
});
Expand Down

0 comments on commit 2e7fefc

Please sign in to comment.