Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages showcasing Symfony with Docker, APIs, queues & async tasks, Webpack, SPAs, etc.

WARNING: You are browsing the documentation for Symfony 3.0 which is not maintained anymore. Consider upgrading your projects to Symfony 5.2.

Using the Logger

3.0 version

Using the Logger

The Console component comes with a standalone logger complying with the PSR-3 standard. Depending on the verbosity setting, log messages will be sent to the Symfony\Component\Console\Output\OutputInterface instance passed as a parameter to the constructor.

The logger does not have any external dependency except php-fig/log. This is useful for console applications and commands needing a lightweight PSR-3 compliant logger:

namespace Acme;

use Psr\Log\LoggerInterface;

class MyDependency
    private $logger;

    public function __construct(LoggerInterface $logger)
        $this->logger = $logger;

    public function doStuff()
        $this->logger->info('I love Tony Vairelles\' hairdresser.');

You can rely on the logger to use this dependency inside a command:

namespace Acme\Console\Command;

use Acme\MyDependency;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Logger\ConsoleLogger;

class MyCommand extends Command
    protected function configure()
                'Use an external dependency requiring a PSR-3 logger'

    protected function execute(InputInterface $input, OutputInterface $output)
        $logger = new ConsoleLogger($output);

        $myDependency = new MyDependency($logger);

The dependency will use the instance of Symfony\Component\Console\Logger\ConsoleLogger as logger. Log messages emitted will be displayed on the console output.


Depending on the verbosity level that the command is run, messages may or may not be sent to the Symfony\Component\Console\Output\OutputInterface instance.

By default, the console logger behaves like the Monolog’s Console Handler. The association between the log level and the verbosity can be configured through the second parameter of the Symfony\Component\Console\ConsoleLogger constructor:

use Psr\Log\LogLevel;
// ...

$verbosityLevelMap = array(
    LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL,
    LogLevel::INFO   => OutputInterface::VERBOSITY_NORMAL,
$logger = new ConsoleLogger($output, $verbosityLevelMap);


The logger outputs the log messages formatted with a color reflecting their level. This behavior is configurable through the third parameter of the constructor:

// ...
$formatLevelMap = array(
    LogLevel::CRITICAL => ConsoleLogger::INFO,
    LogLevel::DEBUG    => ConsoleLogger::ERROR,
$logger = new ConsoleLogger($output, array(), $formatLevelMap);

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.