0

I want to save all of returns "Command" file in a log file

<?php

namespace LogicielBundle\Command\Cron;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MailRapportDometechCommand extends ContainerAwareCommand
{

    protected function configure()
    {
       $this
        ->setName('logiciel:mail_rapport_preparation_dometech')
    ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('. <bg=black;fg=white>Log color ...</>');

        $logFile = fopen('app/CommandLogs/MailRapportDometech/compteur.txt', 'w+');
        fputs($logFile, 'Test ... how to have $output data ?');
        fclose($logFile);

    }
}

And if possible , send me an e -mail error if there :) Thanks :)

2 Answers 2

5

I have faced the same issue, so i solved it like that :

Try to create handlers for specific logging channels to store logs in different files. here i've defined security channel as an example.

/!\ Your Command class must extend ContainerAwareCommand in order to use $this->getContainer() to get this parameter kernel.logs_dir. As a result this line $this->getContainer()->getParameter('kernel.logs_dir') will give you the path to your logs directory.

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

class CheckCommand extends ContainerAwareCommand
{
    protected function execute(InputInterface $input, OutputInterface $output)
    {

        // create a log channel
        $log = new Logger('security');
        $log->pushHandler(new StreamHandler($this->getContainer()->getParameter('kernel.logs_dir').'/file.log', Logger::DEBUG));

        $log->addWarning('Foo');
        $log->addError('Bar');
}

Your log files will be stored at /var/www/html/project-name/var/logs

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

Comments

2

You have to use logger service instead of writing into files directly. Just for these actions Monolog bundle was integrated into Symfony's core.

For example:

$this->getContainer()->get('logger')->error('your message');

And at your app/config/config.yml you can customize handlers for each level.

monolog:
    handlers:
        applog:
            type: stream
            path: /path/to/your/file.log
            level: error

At the documentation you can find how to create email handler.

2 Comments

Thanks very much but this method cannot save PHP error :-/
You are not right. Symfony Debug component handles all errors and writes them to several channels. You can enable it on production without reporting to end-user Debug::enable(null, false);

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.