How to Configure Monolog to Display Console Messages

How to Configure Monolog to Display Console Messages

It is possible to use the console to print messages for certain verbosity levels using the Symfony\Component\Console\Output\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. use Symfony\Component\Console\Input\InputInterface;
  2. use Symfony\Component\Console\Output\OutputInterface;
  3. protected function execute(InputInterface $input, OutputInterface $output)
  4. {
  5. if ($output->isDebug()) {
  6. $output->writeln('Some info');
  7. }
  8. if ($output->isVerbose()) {
  9. $output->writeln('Some more info');
  10. }
  11. }

Instead of using these semantic methods to test for each of the verbosity levels, the MonologBridge provides a Symfony\Bridge\Monolog\Handler\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. // src/Command/YourCommand.php
  2. namespace App\Command;
  3. use Psr\Log\LoggerInterface;
  4. use Symfony\Component\Console\Command\Command;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. class YourCommand extends Command
  8. {
  9. private $logger;
  10. public function __construct(LoggerInterface $logger)
  11. {
  12. $this->logger = $logger;
  13. }
  14. protected function execute(InputInterface $input, OutputInterface $output)
  15. {
  16. $this->logger->debug('Some info');
  17. $this->logger->notice('Some more info');
  18. }
  19. }

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.

LoggerInterfaceVerbosityCommand line
->error()OutputInterface::VERBOSITY_QUIETstderr
->warning()OutputInterface::VERBOSITY_NORMALstdout
->notice()OutputInterface::VERBOSITY_VERBOSE-v
->info()OutputInterface::VERBOSITY_VERY_VERBOSE-vv
->debug()OutputInterface::VERBOSITY_DEBUG-vvv

The Monolog console handler is enabled by default:

  • YAML

    1. # config/packages/dev/monolog.yaml
    2. monolog:
    3. handlers:
    4. # ...
    5. console:
    6. type: console
    7. process_psr_3_messages: false
    8. channels: ['!event', '!doctrine', '!console']
    9. # optionally configure the mapping between verbosity levels and log levels
    10. # verbosity_levels:
    11. # VERBOSITY_NORMAL: NOTICE
  • XML

    1. <!-- config/packages/dev/monolog.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:monolog="http://symfony.com/schema/dic/monolog"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd">
    8. <monolog:config>
    9. <!-- ... -->
    10. <monolog:handler name="console" type="console" process-psr-3-messages="false">
    11. <monolog:channels>
    12. <monolog:channel>!event</monolog:channel>
    13. <monolog:channel>!doctrine</monolog:channel>
    14. <monolog:channel>!console</monolog:channel>
    15. </monolog:channels>
    16. </monolog:handler>
    17. </monolog:config>
    18. </container>
  • PHP

    1. // config/packages/dev/monolog.php
    2. $container->loadFromExtension('monolog', [
    3. 'handlers' => [
    4. 'console' => [
    5. 'type' => 'console',
    6. 'process_psr_3_messages' => false,
    7. 'channels' => ['!event', '!doctrine', '!console'],
    8. ],
    9. ],
    10. ]);

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.