Description
Symfony version(s) affected: 5.0.2
Description
Database queries in listeners to kernel.terminate
event are not logged to profiler, but they are still logged to var/log/dev.log
. I would expect them to be either in both places or none.
I discovered this after I added a listener that saves timestamp of last user activity to database (and I thought it may be a good idea to do it in kernel.terminate
not to block the response).
How to reproduce
- create a Symfony demo application
- create a listener in
src/DemoListener.php
:
<?php declare(strict_types = 1);
namespace App;
use App\Entity\Post;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
final class DemoListener
{
/** @var \Doctrine\ORM\EntityManagerInterface */
private $entityManager;
public function __construct(
EntityManagerInterface $entityManager
)
{
$this->entityManager = $entityManager;
}
public function __invoke(TerminateEvent $event): void
{
$posts = $this->entityManager->createQueryBuilder()
->select('post')
->from(Post::class, 'post')
->getQuery()->getResult();
}
}
- and register in in
services.yaml
:
App\DemoListener:
tags:
- { name: 'kernel.event_listener' }
(You can use the patch listener.patch.txt )
- open any page in the demo application
- the SELECT from the listener is not visible in profiler
- the SELECT from the listener is visible in
var/log/dev.log
Possible Solution
Maybe collect the profiler data in kernel.terminate
? But I guess that would have other consequences.
Additional context
I found an opinion that use of kernel.terminate
is dangerous and should be avoided #27544 (comment) but it is not mentioned in docs.