Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • SensioLabs Professional services to help you with Symfony
    • 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
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Logging
  4. How to Configure Monolog to Display Console Messages
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

How to Configure Monolog to Display Console Messages

Edit this page

How to Configure Monolog to Display Console Messages

It is possible to use the console to print messages for certain verbosity levels using the OutputInterface instance that is passed when a command is run.

When a lot of logging has to happen, it's cumbersome to print information depending on the verbosity settings (-v, -vv, -vvv) because the calls need to be wrapped in conditions. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

protected function execute(InputInterface $input, OutputInterface $output)
{
    if ($output->isDebug()) {
        $output->writeln('Some info');
    }

    if ($output->isVerbose()) {
        $output->writeln('Some more info');
    }
}

Instead of using these semantic methods to test for each of the verbosity levels, the MonologBridge provides a ConsoleHandler that listens to console events and writes log messages to the console output depending on the current log level and the console verbosity.

The example above could then be rewritten as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// src/Command/YourCommand.php
namespace App\Command;

use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class YourCommand extends Command
{
    public function __construct(
        private LoggerInterface $logger,
    ) {
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->logger->debug('Some info');
        $this->logger->notice('Some more info');
    }
}

Depending on the verbosity level that the command is run in and the user's configuration (see below), these messages may or may not be displayed to the console. If they are displayed, they are time-stamped and colored appropriately. Additionally, error logs are written to the error output (php://stderr). There is no need to conditionally handle the verbosity settings anymore.

LoggerInterface Verbosity Command line
->error() OutputInterface::VERBOSITY_QUIET stderr
->warning() OutputInterface::VERBOSITY_NORMAL stdout
->notice() OutputInterface::VERBOSITY_VERBOSE -v
->info() OutputInterface::VERBOSITY_VERY_VERBOSE -vv
->debug() OutputInterface::VERBOSITY_DEBUG -vvv

The Monolog console handler is enabled by default:

1
2
3
4
5
6
7
8
9
10
11
12
# config/packages/dev/monolog.yaml
monolog:
    handlers:
        # ...
        console:
            type:   console
            process_psr_3_messages: false
            channels: ['!event', '!doctrine', '!console']

            # optionally configure the mapping between verbosity levels and log levels
            # verbosity_levels:
            #     VERBOSITY_NORMAL: NOTICE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!-- config/packages/dev/monolog.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:monolog="http://symfony.com/schema/dic/monolog"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd">

    <monolog:config>
        <!-- ... -->

        <monolog:handler name="console" type="console" process-psr-3-messages="false">
            <monolog:channels>
                <monolog:channel>!event</monolog:channel>
                <monolog:channel>!doctrine</monolog:channel>
                <monolog:channel>!console</monolog:channel>
            </monolog:channels>
        </monolog:handler>
    </monolog:config>
</container>
1
2
3
4
5
6
7
8
9
10
// config/packages/dev/monolog.php
use Symfony\Config\MonologConfig;

return static function (MonologConfig $monolog) {
    $monolog->handler('console')
        ->type('console')
        ->processPsr3Messages(false)
        ->channels()->elements(['!event', '!doctrine', '!console'])
    ;
};

Now, log messages will be shown on the console based on the log levels and verbosity. By default (normal verbosity level), warnings and higher will be shown. But in full verbosity mode, all messages will be shown.

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

    Symfony 6.3 is backed by

    Symfony 6.3 is backed by

    Symfony 6.3 is backed by

    Symfony 6.3 is backed by

    Online exam, become Sylius certified today

    Online exam, become Sylius certified today

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

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

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of Gábor Fási, a Symfony contributor

    Thanks Gábor Fási for being a Symfony contributor

    3 commits • 22 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?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • 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

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

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

      Deployed on

    Follow Symfony

    Search by Algolia