Skip to content

Database queries in kernel.terminate are not logged to profiler (only to file) #35382

Closed
@mhujer

Description

@mhujer

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

  1. create a Symfony demo application
  2. 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();
    }
}
  1. and register in in services.yaml:
    App\DemoListener:
        tags:
            - { name: 'kernel.event_listener' }

(You can use the patch listener.patch.txt )

  1. open any page in the demo application
  2. the SELECT from the listener is not visible in profiler
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions