Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log simple debug messages to a file in Symfony?

Tags:

php

symfony

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.

like image 206
jini Avatar asked Aug 03 '12 03:08

jini


1 Answers

Assuming that you are using MonologBundle that is included by default

Step 1

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

Step 2

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

Step 3

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

like image 157
Vitalii Zurian Avatar answered Nov 13 '22 10:11

Vitalii Zurian