I want to be able to write debug messages to a log file. What is the proper way of doing this? There is absolutely no documentation that i can find out there. I know the Monolog bundle is mentioned but it is not writing anything. Please suggest.
Assuming that you are using MonologBundle that is included by default
Define your handler service and pass directory/file there
your.log.handler:
class: %monolog.handler.stream.class%
arguments: [ %kernel.logs_dir%/%kernel.environment%.yourFileName.log ]
Class parameter %monolog.handler.stream.class%
is defined in MonologBundle and it is Monolog\Handler\StreamHandler
class
Define your logger service and inject your handler there
your.logger:
class: %monolog.logger.class%
arguments: [ nameOfLoggingChannel ]
calls: [ [pushHandler, [@your.log.handler]] ]
Parameter %monolog.logger.class%
is also defined in MonologBundle and represents the Symfony\Bridge\Monolog\Logger
class
Inject it to your controller in and use as normal logger
$logger = $this->get('your.logger');
$logger->warn('We are using custom logger');
Check your app/logs/dev.yourFileName.log
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With