How to Enable Logging in Console Commands

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. However, there are cases when you might need
logging. For example, if you are running console commands unattended, such
as from cron jobs or deployment scripts, it may be easier to use Symfony's
logging capabilities instead of configuring other tools to gather console
output and process it. This can be especially handful if you already have
some existing setup for aggregating and analyzing Symfony logs.

This one is really simple. When you create a console command within the full-stack
framework as described in "Console Commands", your command extends
ContainerAwareCommand.
This means that you can simply access the standard logger service through the
container and use it to do the logging:

In the code above, when any command throws an exception, the listener will
receive an event. You can simply log it by passing the logger service via the
service configuration. Your method receives a
ConsoleExceptionEvent object,
which has methods to get information about the event and the exception.

The logging capabilities of the console can be further extended by logging
commands that return error exit statuses, which are any number different than
zero. This way you will know if a command had any errors, even if no exceptions
were thrown.

First configure a listener for console terminate events in the service container: