setSQLLogger is deprecated, how can I disable the logger? #10147
-
I used to disable logging in some case using But now, How can I disable the logger? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 16 replies
-
If you use the the middleware, this concern has moved to the logger implementation. In other words, use a PSR-3 logger that you can disable. |
Beta Was this translation helpful? Give feedback.
This comment has been hidden.
This comment has been hidden.
-
To remove the Logging Middleware I use: $conf = $em->getConnection()->getConfiguration();
$mids = $conf->getMiddlewares();
$mids = array_filter($mids, fn($mid) => !($mid instanceof \Doctrine\DBAL\Logging\Middleware));
$conf->setMiddlewares($mids);
Edit: Beware if you're doing this because of Bulk Inserting using persist. I really hope they create a standard way of enforcing bulk inserting rather than having to dance around it like this. Because in the future if they add more Middleware that were to interfere, you'd have another problem using this technique. You could of course |
Beta Was this translation helpful? Give feedback.
-
To follow up on my previous answer: If you use Monolog in your application, your Monolog and DBAL setup probably looks like this: use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Logging\Middleware as LoggingMiddleware;
use Monolog\Logger;
$logger = new Logger('doctrine');
// Your log handlers are configured here.
$configuration = new Configuration();
$configuration->setMiddlewares([new LoggingMiddleware($logger)]);
// More configuration …
$connection = DriverManager::getConnection(
[/* Your connection params go here */],
$configuration,
); Your mileage may vary because bootstrapping Monolog and DBAL might be abstracted away from you by your framework. For instance, if you use Symfony with DoctrineBundle and MonologBundle, you will likely have a logger registered as service If you never want to log anything, just don't wire the middleware. Done. Strongly recommended for production. In Symfony, you achieve this by setting Now, if you just want to disable logging temporarily, you can use a switch like this: use Monolog\Handler\NullHandler;
use Monolog\Logger;
final readonly class LogSwitch
{
public function __construct(
private Logger $logger,
) {
}
/**
* @param callable(): TReturn $callable
* @return TReturn
*
* @template TReturn of mixed
*/
public function withLoggingDisabled(callable $callable)
{
$this->logger->pushHandler(new NullHandler());
try {
return $callable();
} finally {
$this->logger->popHandler();
}
}
} As you can see, this switch has no idea what a database connection is. It's completely agnostic to the reasons why we want to disable a logger. You can pass a closure to the only method it exposes and it will execute it while making sure that the given logger is not doing anything while the closure is executed. // Make sure, $logger is the same logger that we passed to the logging middleware!
$logSwitch = new LogSwitch($logger);
// This query will be logged
$connection->executeQuery('SELECT 1');
$logSwitch->withLoggingDisabled(static function () use ($connection) {
// This query won't appear in the logs!
$connection->executeQuery('SELECT 2;');
});
// This query will be logged
$connection->executeQuery('SELECT 1'); tl;dr: Disable the actual logger, not the middleware. |
Beta Was this translation helpful? Give feedback.
If you use the the middleware, this concern has moved to the logger implementation. In other words, use a PSR-3 logger that you can disable.