|
| 1 | +.. index:: |
| 2 | + single: Logging; Console messages |
| 3 | + |
| 4 | +How to Configure Monolog to Display Console Messages |
| 5 | +==================================================== |
| 6 | + |
| 7 | +It is possible to use the console to print messages for a certain :ref:`verbosity-levels` |
| 8 | +using the :class:`Symfony\\Component\\Console\\Output\\OutputInterface` |
| 9 | +instance that is passed when a command gets executed. |
| 10 | + |
| 11 | +When a lot of logging has to happen, it's cumbersome to print information |
| 12 | +depending on the verbosity settings (``-v``, ``-vv``, ``-vvv``) because the |
| 13 | +calls need to be wrapped in conditions. The code quickly gets verbose or dirty. |
| 14 | +For example:: |
| 15 | + |
| 16 | + use Symfony\Component\Console\Input\InputInterface; |
| 17 | + use Symfony\Component\Console\Output\OutputInterface; |
| 18 | + |
| 19 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 20 | + { |
| 21 | + if ($output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) { |
| 22 | + $output->writeln('Some info'); |
| 23 | + } |
| 24 | + |
| 25 | + if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
| 26 | + $output->writeln('Some more info'); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | +Instead of using these semantic methods to test for each of the verbosity |
| 31 | +levels, `MonologBundle`_ 2.4 provides a `ConsoleHandler`_ that listens to |
| 32 | +console events and writes log messages to the console output depending on the |
| 33 | +current log level and the console verbosity. |
| 34 | + |
| 35 | +The example above could then be rewritten as:: |
| 36 | + |
| 37 | + use Symfony\Component\Console\Input\InputInterface; |
| 38 | + use Symfony\Component\Console\Output\OutputInterface; |
| 39 | + |
| 40 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 41 | + { |
| 42 | + $logger->debug('Some info'); |
| 43 | + |
| 44 | + $logger->notice('Some more info'); |
| 45 | + } |
| 46 | + |
| 47 | +These messages will get displayed on the console, timestamped, colored |
| 48 | +depending on the log level and error logs are written to the error output |
| 49 | +(php://stderr). There is no need to conditionally handle the verbosity |
| 50 | +settings anymore. |
| 51 | + |
| 52 | +The Monolog console handler is enabled in the Monolog configuration. This is |
| 53 | +the default in Symfony Standard Edition 2.4 too. |
| 54 | + |
| 55 | +.. configuration-block:: |
| 56 | + |
| 57 | + .. code-block:: yaml |
| 58 | +
|
| 59 | + # app/config/config.yml |
| 60 | + monolog: |
| 61 | + handlers: |
| 62 | + console: |
| 63 | + type: console |
| 64 | +
|
| 65 | + .. code-block:: xml |
| 66 | +
|
| 67 | + <!-- app/config/config.xml --> |
| 68 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 69 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 70 | + xmlns:monolog="http://symfony.com/schema/dic/monolog"> |
| 71 | +
|
| 72 | + <monolog:config> |
| 73 | + <monolog:handler name="console" type="console" /> |
| 74 | + </monolog:config> |
| 75 | + </container> |
| 76 | +
|
| 77 | + .. code-block:: php |
| 78 | +
|
| 79 | + // app/config/config.php |
| 80 | + $container->loadFromExtension('monolog', array( |
| 81 | + 'handlers' => array( |
| 82 | + 'console' => array( |
| 83 | + 'type' => 'console', |
| 84 | + ), |
| 85 | + ), |
| 86 | + )); |
| 87 | +
|
| 88 | +With the ``verbosity_levels`` option you can adapt the mapping between |
| 89 | +verbosity and log level. In the given example it will also show notices in |
| 90 | +normal verbosity mode (instead of warnings only). Additionally, it will only |
| 91 | +use messages logged with the custom ``my_channel`` channel and it changes the |
| 92 | +display style via a custom formatter. See also the :doc:`reference/configuration/monolog` |
| 93 | +for more information: |
| 94 | + |
| 95 | +.. configuration-block:: |
| 96 | + |
| 97 | + .. code-block:: yaml |
| 98 | +
|
| 99 | + # app/config/config.yml |
| 100 | + monolog: |
| 101 | + handlers: |
| 102 | + console: |
| 103 | + type: console |
| 104 | + verbosity_levels: |
| 105 | + VERBOSITY_NORMAL: NOTICE |
| 106 | + channels: my_channel |
| 107 | + formatter: my_formatter |
| 108 | +
|
| 109 | + .. code-block:: xml |
| 110 | +
|
| 111 | + <!-- app/config/config.xml --> |
| 112 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 113 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 114 | + xmlns:monolog="http://symfony.com/schema/dic/monolog"> |
| 115 | +
|
| 116 | + <monolog:config> |
| 117 | + <monolog:handler name="console" type="console" formatter="my_formatter"> |
| 118 | + <monolog:verbosity-level verbosity-normal="NOTICE" /> |
| 119 | + <monolog:channel>my_channel</monolog:channel> |
| 120 | + </monolog:handler> |
| 121 | + </monolog:config> |
| 122 | + </container> |
| 123 | +
|
| 124 | + .. code-block:: php |
| 125 | +
|
| 126 | + // app/config/config.php |
| 127 | + $container->loadFromExtension('monolog', array( |
| 128 | + 'handlers' => array( |
| 129 | + 'console' => array( |
| 130 | + 'type' => 'console', |
| 131 | + 'verbosity_levels' => array( |
| 132 | + 'VERBOSITY_NORMAL' => 'NOTICE', |
| 133 | + ), |
| 134 | + 'channels' => 'my_channel', |
| 135 | + 'formatter' => 'my_formatter', |
| 136 | + ), |
| 137 | + ), |
| 138 | + )); |
| 139 | +
|
| 140 | +.. configuration-block:: |
| 141 | + |
| 142 | + .. code-block:: yaml |
| 143 | +
|
| 144 | + # app/config/services.yml |
| 145 | + services: |
| 146 | + my_formatter: |
| 147 | + class: Symfony\Bridge\Monolog\Formatter\ConsoleFormatter |
| 148 | + arguments: |
| 149 | + - "[%%datetime%%] %%start_tag%%%%message%%%%end_tag%% (%%level_name%%) %%context%% %%extra%%\n" |
| 150 | +
|
| 151 | + .. code-block:: xml |
| 152 | +
|
| 153 | + <!-- app/config/services.xml --> |
| 154 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 155 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 156 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 157 | + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 158 | +
|
| 159 | + <services> |
| 160 | + <service id="my_formatter" class="Symfony\Bridge\Monolog\Formatter\ConsoleFormatter"> |
| 161 | + <argument>[%%datetime%%] %%start_tag%%%%message%%%%end_tag%% (%%level_name%%) %%context%% %%extra%%\n</argument> |
| 162 | + </service> |
| 163 | + </services> |
| 164 | +
|
| 165 | + </container> |
| 166 | +
|
| 167 | + .. code-block:: php |
| 168 | +
|
| 169 | + // app/config/services.php |
| 170 | + $container |
| 171 | + ->register('my_formatter', 'Symfony\Bridge\Monolog\Formatter\ConsoleFormatter') |
| 172 | + ->addArgument('[%%datetime%%] %%start_tag%%%%message%%%%end_tag%% (%%level_name%%) %%context%% %%extra%%\n') |
| 173 | + ; |
| 174 | +
|
| 175 | +.. _ConsoleHandler: https://github.com/symfony/MonologBridge/blob/master/Handler/ConsoleHandler.php |
| 176 | +.. _MonologBundle: https://github.com/symfony/MonologBundle |
0 commit comments