Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
SymfonyOnline January 2025
January 16-17
100% Online
+25 talks and workshops
  1. Home
  2. Documentation
  3. Console
  4. Using the Logger

Using the Logger

Edit this page

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 OutputInterface instance passed as a parameter to the constructor.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
namespace Acme;

use Psr\Log\LoggerInterface;

class MyDependency
{
    public function __construct(
        private LoggerInterface $logger,
    ) {
    }

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
namespace Acme\Console\Command;

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

#[AsCommand(
    name: 'my:command',
    description: 'Use an external dependency requiring a PSR-3 logger'
)]
class MyCommand extends Command
{
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $logger = new ConsoleLogger($output);

        $myDependency = new MyDependency($logger);
        $myDependency->doStuff();

        return Command::SUCCESS;
    }
}

The dependency will use the instance of ConsoleLogger as logger. Log messages emitted will be displayed on the console output.

Verbosity

Depending on the verbosity level that the command is run, messages may or may not be sent to the 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 ConsoleLogger constructor:

1
2
3
4
5
6
7
8
9
use Psr\Log\LogLevel;
// ...

$verbosityLevelMap = [
    LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL,
    LogLevel::INFO   => OutputInterface::VERBOSITY_NORMAL,
];

$logger = new ConsoleLogger($output, $verbosityLevelMap);

Color

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

1
2
3
4
5
6
7
// ...
$formatLevelMap = [
    LogLevel::CRITICAL => ConsoleLogger::ERROR,
    LogLevel::DEBUG    => ConsoleLogger::INFO,
];

$logger = new ConsoleLogger($output, [], $formatLevelMap);

Errors

The Console logger includes a hasErrored() method which returns true as soon as any error message has been logged during the execution of the command. This is useful to decide which status code to return as the result of executing the command.

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

    Symfony 7.1 is backed by

    Code consumes server resources. Blackfire tells you how

    Code consumes server resources. Blackfire tells you how

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Version:

    Table of Contents

    • Verbosity
    • Color
    • Errors

    Symfony footer

    Avatar of Piergiuseppe Longo, a Symfony contributor

    Thanks Piergiuseppe Longo for being a Symfony contributor

    1 commit • 3 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony