When you configure Doctrine to log queries in Symfony, the expectation is often that it works out of the box because the underlying driver supports it. However, relying on the default doctrine channel without configuring the correct logger instance results in silence, even when your application logs are otherwise flowing correctly. The issue isn't usually the database connection or the config.yml flags like logging: true; those simply enable the profiler and query logging hooks within Doctrine itself. The real bottleneck is how Symfony's Monolog component interprets that request.
In the provided configuration, the doctrine handler in config_dev.yml correctly defines a stream writer pointing to %kernel.logs_dir%/%kernel.environment%_doctrine.log. However, this handler is only effective if you explicitly register it as a service and tag it with the monolog.logger.doctrine alias. By default, Symfony registers a logger for every channel mentioned in your configuration, but often the specific doctrine logger needs to be instantiated to match the handler's expectations. If you are using an older version of Symfony or have not updated your kernel parameters, the system might be ignoring the channel because no service is bound to handle that specific log name.
To fix this without writing a custom class, you need to ensure the Doctrine logger is registered in your services configuration. In modern Symfony applications (2.x and beyond), you can achieve this by defining the logger in config_dev.yml or via a dedicated service definition file. The key is to map the channel doctrine to your stream handler explicitly.
# config_dev.yml or services_dev.yml
services:
monolog.logger.doctrine:
class: Monolog\Logger
decorates: monolog
arguments: [ '@monolog.handler.doctrine' ]
tags: [ { name: 'monolog.channel', channel: 'doctrine' } ]
Alternatively, if you prefer to stick purely to configuration files and are using a standard setup, ensure that the doctrine handler is added to the main Monolog service definition. The trick often lies in the order of operations or the specific version of Doctrine bundled with your Symfony release. Some versions require the logger to be initialized before the EntityManager is instantiated.
If you find that configuring the channel still yields no results, consider implementing a custom SQL Logger class, as suggested in external resources like this guide from LaravelCompany. While writing a custom logger offers granular controlâallowing you to filter queries by entity, format output differently, or send logs to specific sinksâit adds complexity. You would need to create a class extending Doctrine\DBAL\Logging\SQLLogger, implement the logging logic, and then register it as a service with the appropriate tags (monolog.logger.doctrine or similar, depending on your version).
The trade-off here is significant. A custom logger gives you flexibility but requires more maintenance. The default Monolog integration is simpler but can be finicky if the service container isn't wired up correctly. Another common pitfall is forgetting to clear your cache (php bin/console cache:clear) after making changes to config_dev.yml or services, which often leads developers to believe their configuration changes haven't taken effect when they actually have.
Finally, verify that you are running in debug mode. The logging flag in doctrine.dbal is respected primarily in development environments. If your application is running on a production server where kernel.debug is set to false, the logging behavior may be suppressed entirely regardless of your configuration files. Ensure that the environment variable or parameter controlling this state matches your expectations before concluding that the setup is broken.