Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 log command output

Tags:

symfony

Is it possible to log a console command`s output to a log file ? For example when a command has:

protected function execute(InputInterface $input, OutputInterface $output)
{
    ...
    $output->writeln('command run');
    ...
}

How to log the output without modifying the execute method ?

like image 624
dima Avatar asked Sep 13 '26 18:09

dima


1 Answers

According to Symfony2 documentation:

The Console component doesn't provide any logging capabilities out of the box. Normally, you run console commands manually and observe the output, which is why logging is not provided.

http://symfony.com/doc/2.8/console/logging.html

However you could use service container inside the command:

$logger = $this->getContainer()->get('logger');

And log some events:

$logger->info('Hello: '.$variable);

You should see the logged entries in app/logs/dev.log or app/logs/prod.log.

like image 76
rescobar Avatar answered Sep 17 '26 20:09

rescobar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!