Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Logging
  4. How to Configure Monolog to Display Console Messages
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

How to Configure Monolog to Display Console Messages

Edit this page

Warning: You are browsing the documentation for Symfony 3.0, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

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 gets executed.

See also

Alternatively, you can use the standalone PSR-3 logger provided with the console component.

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. The code quickly gets verbose or dirty. 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->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) {
        $output->writeln('Some info');
    }

    if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
        $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
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

protected function execute(InputInterface $input, OutputInterface $output)
{
    // assuming the Command extends ContainerAwareCommand...
    $logger = $this->getContainer()->get('logger');
    $logger->debug('Some info');

    $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 timestamped 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.

The Monolog console handler is enabled in the Monolog configuration.

  • YAML
  • XML
  • PHP
1
2
3
4
5
# app/config/config.yml
monolog:
    handlers:
        console:
            type: console
1
2
3
4
5
6
7
8
9
<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:monolog="http://symfony.com/schema/dic/monolog">

    <monolog:config>
        <monolog:handler name="console" type="console" />
    </monolog:config>
</container>
1
2
3
4
5
6
7
8
// app/config/config.php
$container->loadFromExtension('monolog', array(
    'handlers' => array(
        'console' => array(
           'type' => 'console',
        ),
    ),
));

With the verbosity_levels option you can adapt the mapping between verbosity and log level. In the given example it will also show notices in normal verbosity mode (instead of warnings only). Additionally, it will only use messages logged with the custom my_channel channel and it changes the display style via a custom formatter (see the MonologBundle reference for more information):

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
7
8
9
# app/config/config.yml
monolog:
    handlers:
        console:
            type:   console
            verbosity_levels:
                VERBOSITY_NORMAL: NOTICE
            channels: my_channel
            formatter: my_formatter
1
2
3
4
5
6
7
8
9
10
11
12
<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:monolog="http://symfony.com/schema/dic/monolog">

    <monolog:config>
        <monolog:handler name="console" type="console" formatter="my_formatter">
            <monolog:verbosity-level verbosity-normal="NOTICE" />
            <monolog:channel>my_channel</monolog:channel>
        </monolog:handler>
    </monolog:config>
</container>
1
2
3
4
5
6
7
8
9
10
11
12
13
// app/config/config.php
$container->loadFromExtension('monolog', array(
    'handlers' => array(
        'console' => array(
            'type' => 'console',
            'verbosity_levels' => array(
                'VERBOSITY_NORMAL' => 'NOTICE',
            ),
            'channels' => 'my_channel',
            'formatter' => 'my_formatter',
        ),
    ),
));
  • YAML
  • XML
  • PHP
1
2
3
4
5
6
# app/config/services.yml
services:
    my_formatter:
        class: Symfony\Bridge\Monolog\Formatter\ConsoleFormatter
        arguments:
            - "[%%datetime%%] %%start_tag%%%%message%%%%end_tag%% (%%level_name%%) %%context%% %%extra%%\n"
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- app/config/services.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"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

     <services>
        <service id="my_formatter" class="Symfony\Bridge\Monolog\Formatter\ConsoleFormatter">
            <argument>[%%datetime%%] %%start_tag%%%%message%%%%end_tag%% (%%level_name%%) %%context%% %%extra%%\n</argument>
        </service>
     </services>

</container>
1
2
3
4
5
// app/config/services.php
$container
    ->register('my_formatter', 'Symfony\Bridge\Monolog\Formatter\ConsoleFormatter')
    ->addArgument('[%%datetime%%] %%start_tag%%%%message%%%%end_tag%% (%%level_name%%) %%context%% %%extra%%\n')
;
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
No stress: we've got you covered with our 116 automated quality checks of your code

No stress: we've got you covered with our 116 automated quality checks of your code

Code consumes server resources. Blackfire tells you how

Code consumes server resources. Blackfire tells you how

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

Avatar of Barun, a Symfony contributor

Thanks Barun for being a Symfony contributor

1 commit • 20 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