Method setSQLLogger deprecated - Doctrine\DBAL\Configuration

3.2k Views Asked by At

I am in the process of upgrading an application from Symfony 5.4 to Symfony 6.0. Along the way, I had to upgrade some doctrine libraries.

We are currently using setSQLLogger(null) to avoid having SQL logging enabled. By using the newer version of Doctrine, I am getting a warning:

The Doctrine\DBAL\Configuration::setSQLLogger method is deprecated (Use {@see setMiddlewares()} and {@see \Doctrine\DBAL\Logging\Middleware} instead.).

I could not figure out how can I replace setSQLLogger(null) with setMiddlewares so I could disable the SQL logging.

Did anyone have this issue and managed to fix it?

Thanks.

3

There are 3 best solutions below

0
DonCallisto On

You should configure a middleware in order to accept the NullLogger, then use it along with setMiddlewares method.

So from symfony standpoint, you can do something like

# configuration.yaml // or whatever name you have
services:
  doctrine.logging.middleware.null: // or whatever name you prefer
    class: Doctrine\DBAL\Logging\Middleware
      autowire: false
      arguments:
        - #FQCN or service id of NullLogger

Then you can inject it where you were using setLogger and replace that method call with setMiddlewares.

I didn't tried it myself, as we're running on older version, but I'm pretty confident this should resolve your issue.

1
Jorr.it On

I replaced this code:

$em->getConnection()->getConfiguration()->setSQLLogger(null);

With:

$em->getConnection()->getConfiguration()->setMiddlewares([new \Doctrine\DBAL\Logging\Middleware(new \Psr\Log\NullLogger())]);

This puts the NullLogger as the only middleware.

0
dominic detta On

If you are using the package roave/psr-container-doctrine you can register new middleware in your autoload config using the associated key middlewares

Then you create a new service which build an instance of Doctrine\DBAL\Logging\Middleware.

Example building the service with Monolog logger:

class DoctrineMiddlewareLoggerFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
    {

        $logger = new Logger('doctrine');
        $logger->pushHandler(new StreamHandler('logs/doctrine.log', Logger::DEBUG));
        $logger->pushProcessor(new PsrLogMessageProcessor(null, true));
        return new Middleware($logger);
    }
}

Wire the service with your factory:

return [
            'factories' => [
                'doctrine.middleware.logger' => DoctrineMiddlewareLoggerFactory::class,

Configure your middleware in the configuration key:

 'configuration'=>[
            'orm_default'=>[
                'middlewares'=>[
                    'doctrine.middleware.logger'
                ]
            ]
        ],