8000 Merge branch '6.3' into 6.4 · symfony/symfony-docs@60bd0ee · GitHub
[go: up one dir, main page]

Skip to content

Commit 60bd0ee

Browse files
committed
Merge branch '6.3' into 6.4
* 6.3: Fix a code example [Logger] Switching to more modern config format
2 parents 0f6360c + 9d6745b commit 60bd0ee

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

logging/channels_handlers.rst

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,27 @@ Switching a Channel to a different Handler
2323
Now, suppose you want to log the ``security`` channel to a different file.
2424
To do this, create a new handler and configure it to log only messages
2525
from the ``security`` channel. The following example does that only in the
26-
``prod`` :ref:`configuration environment <configuration-environments>` but you
27-
can do it in any (or all) environments:
26+
``prod`` :ref:`configuration environment <configuration-environments>`:
2827

2928
.. configuration-block::
3029

3130
.. code-block:: yaml
3231
33-
# config/packages/prod/monolog.yaml
34-
monolog:
35-
handlers:
36-
security:
37-
# log all messages (since debug is the lowest level)
38-
level: debug
39-
type: stream
40-
path: '%kernel.logs_dir%/security.log'
41-
channels: [security]
42-
43-
# an example of *not* logging security channel messages for this handler
44-
main:
45-
# ...
46-
# channels: ['!security']
32+
# config/packages/monolog.yaml
33+
when@prod:
34+
monolog:
35+
handlers:
36+
security:
37+
# log all messages (since debug is the lowest level)
38+
level: debug
39+
type: stream
40+
path: '%kernel.logs_dir%/security.log'
41+
channels: [security]
42+
43+
# an example of *not* logging security channel messages for this handler
44+
main:
45+
# ...
46+
# channels: ['!security']
4747
4848
.. code-block:: xml
4949
@@ -56,12 +56,15 @@ can do it in any (or all) environments:
5656
http://symfony.com/schema/dic/monolog
5757
https://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
5858
59-
<monolog:config>
60-
<monolog:handler name="security" type="stream" path="%kernel.logs_dir%/security.log">
61-
<monolog:channels>
62-
<monolog:channel>security</monolog:channel>
63-
</monolog:channels>
64-
</monolog:handler>
59+
<when env="prod">
60+
<monolog:config>
61+
<monolog:handler name="security" type="stream" path="%kernel.logs_dir%/security.log">
62+
<monolog:channels>
63+
<monolog:channel>security</monolog:channel>
64+
</monolog:channels>
65+
</monolog:handler>
66+
</monolog:config>
67+
</when>
6568
6669
<monolog:handler name="main" type="stream" path="%kernel.logs_dir%/main.log">
6770
<!-- ... -->
@@ -75,18 +78,21 @@ can do it in any (or all) environments:
7578
.. code-block:: php
7679
7780
// config/packages/prod/monolog.php
81+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
7882
use Symfony\Config\MonologConfig;
7983
80-
return static function (MonologConfig $monolog): void {
81-
$monolog->handler('security')
82-
->type('stream')
83-
->path('%kernel.logs_dir%/security.log')
84-
->channels()->elements(['security']);
84+
return static function (MonologConfig $monolog, ContainerConfigurator $container) {
85+
if ('prod' === $container->env()) {
86+
$monolog->handler('security')
87+
->type('stream')
88+
->path(param('kernel.logs_dir') . \DIRECTORY_SEPARATOR . 'security.log')
89+
->channels()->elements(['security']);
8590
86-
$monolog->handler('main')
87-
// ...
91+
$monolog->handler('main')
92+
// ...
8893
89-
->channels()->elements(['!security']);
94+
->channels()->elements(['!security']);
95+
}
9096
};
9197
9298
.. caution::
@@ -131,13 +137,13 @@ You can also configure additional channels without the need to tag your services
131137

132138
.. code-block:: yaml
133139
134-
# config/packages/prod/monolog.yaml
140+
# config/packages/monolog.yaml
135141
monolog:
136142
channels: ['foo', 'bar', 'foo_bar']
137143
138144
.. code-block:: xml
139145
140-
<!-- config/packages/prod/monolog.xml -->
146+
<!-- config/packages/monolog.xml -->
141147
<container xmlns="http://symfony.com/schema/dic/services"
142148
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
143149
xmlns:monolog="http://symfony.com/schema/dic/monolog"
@@ -155,7 +161,7 @@ You can also configure additional channels without the need to tag your services
155161
156162
.. code-block:: php
157163
158-
// config/packages/prod/monolog.php
164+
// config/packages/monolog.php
159165
use Symfony\Config\MonologConfig;
160166
161167
return static function (MonologConfig $monolog): void {

0 commit comments

Comments
 (0)
0