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.
2 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
8 Comments
Strong Like Bull
InvalidArgumentException: There is no extension able to load the configuration for "log_handler" (in /Users/myuser/Sites/myapp.com/app/config/config.yml). Looked for namespace "your.log.handler", found "framework", "security", "twig", "monolog", "swiftmailer", "doctrine", "assetic", "sensio_framework_extra", "jms_security_extra", "fos_user", "fos_rest", "jms_serializer", "main_user", "main_classified", "doctrine_mongodb", "acme_demo", "web_profiler", "sensio_distribution"
Strong Like Bull
sorry Im fairly new to Symfony @thecatontheflat
Strong Like Bull
oops had to put steps1 and step 2 under the services
Strong Like Bull
now I get: ParseException: Unable to parse in "\/Users\/myuser\/Sites\/myapp.com\/app\/config\/config.yml" at line 110 (near " arguments: [ nameOfLoggingChannel ]").
Vitalii Zurian
@jini could you create a chat, so I'll instruct you? :)
|