Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4834b77

Browse files
tvlooyweaverryan
authored andcommitted
How to Configure Monolog to display Console messages
1 parent69f8a88 commit4834b77

File tree

6 files changed

+199
-2
lines changed

6 files changed

+199
-2
lines changed

‎components/console/introduction.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ You can also set these colors and options inside the tagname::
160160
// bold text on a yellow background
161161
$output->writeln('<bg=yellow;options=bold>foo</bg=yellow;options=bold>');
162162

163+
.. verbosity-levels:
164+
163165
Verbosity Levels
164166
~~~~~~~~~~~~~~~~
165167

@@ -226,6 +228,11 @@ When the quiet level is used, all output is suppressed as the default
226228
:method:`Symfony\Component\Console\Output::write <Symfony\\Component\\Console\\Output::write>`
227229
method returns without actually printing.
228230

231+
..tip::
232+
233+
You can use `MonologBundle`_ 2.4 to display messages on the console. This
234+
is cleaner than wrapping your output calls in conditions (see:doc:/cookbook/logging/monolog_console).
235+
229236
Using Command Arguments
230237
-----------------------
231238

@@ -520,3 +527,4 @@ Learn More!
520527

521528
.. _Packagist:https://packagist.org/packages/symfony/console
522529
.. _ANSICON:https://github.com/adoxa/ansicon/downloads
530+
.. _MonologBundle:https://github.com/symfony/MonologBundle

‎cookbook/logging/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ Logging
66

77
monolog
88
monolog_email
9+
monolog_console
910
monolog_regex_based_excludes
1011
channels_handlers

‎cookbook/logging/monolog.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ your controller::
2323
}
2424

2525
The ``logger`` service has different methods for different logging levels.
26-
See:class:`Symfony\\Component\\HttpKernel\\Log\\LoggerInterface` for details
27-
on which methods are available.
26+
SeeLoggerInterface_ for details on which methods are available.
2827

2928
Handlers and Channels: Writing logs to different Locations
3029
----------------------------------------------------------
@@ -351,3 +350,4 @@ using a processor.
351350
handler level instead of globally.
352351

353352
.. _Monolog:https://github.com/Seldaek/monolog
353+
.. _LoggerInterface:https://github.com/php-fig/log/blob/master/Psr/Log/LoggerInterface.php

‎cookbook/logging/monolog_console.rst

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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+
<containerxmlns="http://symfony.com/schema/dic/services"
70+
xmlns:monolog="http://symfony.com/schema/dic/monolog">
71+
72+
<monolog:config>
73+
<monolog:handlername="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+
<containerxmlns="http://symfony.com/schema/dic/services"
114+
xmlns:monolog="http://symfony.com/schema/dic/monolog">
115+
116+
<monolog:config>
117+
<monolog:handlername="console"type="console"formatter="my_formatter">
118+
<monolog:verbosity-levelverbosity-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+
<containerxmlns="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+
<serviceid="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

‎cookbook/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101

102102
* :doc:`/cookbook/logging/monolog`
103103
* :doc:`/cookbook/logging/monolog_email`
104+
* :doc:`/cookbook/logging/monolog_console`
104105
* :doc:`/cookbook/logging/monolog_regex_based_excludes`
105106
* :doc:`/cookbook/logging/channels_handlers`
106107

‎reference/configuration/monolog.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ MonologBundle Configuration ("monolog")
2525
action_level:WARNING
2626
buffer_size:30
2727
handler:custom
28+
console:
29+
type:console
30+
verbosity_levels:
31+
VERBOSITY_NORMAL:WARNING
32+
VERBOSITY_VERBOSE:NOTICE
33+
VERBOSITY_VERY_VERBOSE:INFO
34+
VERBOSITY_DEBUG:DEBUG
2835
custom:
2936
type:service
3037
id:my_handler
@@ -84,6 +91,10 @@ MonologBundle Configuration ("monolog")
8491
action-level="warning"
8592
handler="custom"
8693
/>
94+
<monolog:handler
95+
name="console"
96+
type="console"
97+
/>
8798
<monolog:handler
8899
name="custom"
89100
type="service"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp