Symfony 2 : Log into a specific file
You have to add the info to the services.yml file, not the config.yml file:
So in services.yml
(or services.xml
) you have
my_service.logger: class: Symfony\Bridge\Monolog\Logger arguments: [app] calls: - [pushHandler, [@my_service.logger_handler]]my_service.logger_handler: class: Monolog\Handler\StreamHandler arguments: [%kernel.logs_dir%/%kernel.environment%.admin.log, 200]
and in your controller action you use:
$logger = $this->get('my_service.logger');
The answer above worked perfectly fine for me. I had to adapt it though as my configuration was in xml and not yaml.
Here is the exact same configuration but using xml.
<service id="my_service.logger" class="Symfony\Bridge\Monolog\Logger"> <argument type="string">app</argument> <call method="pushHandler"> <argument type="service" id="my_service.logger_handler" /> </call></service><service id="my_service.logger_handler" class="Monolog\Handler\StreamHandler"> <argument>type="string">%kernel.logs_dir%/%kernel.environment%.license.log</argument> <argument type="string">200</argument></service>