The Console component doesn't provide any logging capabilities out of the box because you normally run console commands yourself and observe the output. However, there are cases when you might need logging; for example, when running console commands unattended, such as from Cron jobs or deployment scripts.
In Symfony 3.2 you had to create your own event listener or subscriber to
subscribe to the console.exception
event and log the exception details.
In Symfony 3.3 we decided to enable automatic exception logging for console
commands.
Therefore, when an exception occurs during the execution of a command, you'll see a message like the following in your log file:
1 2 3 4 5
[2017-02-15 09:34:42] app.ERROR: Exception thrown while running command:
"cache:clear -vvv". Message: "An error occured!" {"exception":"[object]
(RuntimeException(code: 0): An error occured! at vendor/symfony/symfony/
src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php:61)",
"command":"cache:clear -vvv","message":"An error occured!"} []
In addition to logging exceptions, the new subscriber also listens to the
console.terminate
event to add a log message whenever a command doesn't
finish with the 0
exit status.
Nice. And for a formatted output to the logstash ?
@Michael the listener relies on PSR-3, and pass meaningful things in the context (you can see the content encoded as json in the output above btw). So to integrate with Logstash, it is a matter of configuring your logger for that (as any other log message in Symfony). and Monolog has some features for that
Good job, guys! Thank you!
Great. Thanks @Christophe.