Florin Gheorghe
Florin Gheorghe

Reputation: 41

Method setSQLLogger deprecated - Doctrine\DBAL\Configuration

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?

Upvotes: 3

Views: 5094

Answers (4)

Alexander Schranz
Alexander Schranz

Reputation: 2460

I disabled the middlewares via now:

    $middlewares = [];
    foreach ($this->entityManager->getConnection()->getConfiguration()->getMiddlewares() as $middleware) {
        if ($middleware instanceof \Doctrine\DBAL\Logging\Middleware
            || $middleware instanceof \Doctrine\Bundle\DoctrineBundle\Middleware\DebugMiddleware
        ) {
            continue;
        }

        $middlewares[] = $middleware;
    }

    $this->entityManager->getConnection()->getConfiguration()->setMiddlewares($middlewares);

Upvotes: 0

dominic detta
dominic detta

Reputation: 221

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'
                ]
            ]
        ],

Upvotes: 0

Jorr.it
Jorr.it

Reputation: 1320

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.

Upvotes: 7

DonCallisto
DonCallisto

Reputation: 29942

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.

Upvotes: 0

Related Questions