6

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

2 Answers 2

14

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

Sign up to request clarification or add additional context in comments.

8 Comments

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"
sorry Im fairly new to Symfony @thecatontheflat
oops had to put steps1 and step 2 under the services
now I get: ParseException: Unable to parse in "\/Users\/myuser\/Sites\/myapp.com\/app\/config\/config.yml" at line 110 (near " arguments: [ nameOfLoggingChannel ]").
@jini could you create a chat, so I'll instruct you? :)
|
1

add 2 use classes

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

then in function simple use this

$log = new Logger('shipment');
$log->pushHandler(new StreamHandler('var/log/fileName.log', Logger::WARNING));

// add records to the log
$log->addWarning('qty is 0', ['data' => 3]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.