@@ -23,27 +23,27 @@ Switching a Channel to a different Handler
2323Now, suppose you want to log the ``security `` channel to a different file.
2424To do this, create a new handler and configure it to log only messages
2525from 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,14 @@ 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+ </when >
6567
6668 <monolog : handler name =" main" type =" stream" path =" %kernel.logs_dir%/main.log" >
6769 <!-- ... -->
@@ -75,18 +77,21 @@ can do it in any (or all) environments:
7577 .. code-block :: php
7678
7779 // config/packages/prod/monolog.php
80+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
7881 use Symfony\Config\MonologConfig;
7982
80- return static function (MonologConfig $monolog) {
81- $monolog->handler('security')
82- ->type('stream')
83- ->path('%kernel.logs_dir%/security.log')
84- ->channels()->elements(['security']);
83+ return static function (MonologConfig $monolog, ContainerConfigurator $container) {
84+ if ('prod' === $container->env()) {
85+ $monolog->handler('security')
86+ ->type('stream')
87+ ->path(param('kernel.logs_dir') . \DIRECTORY_SEPARATOR . 'security.log')
88+ ->channels()->elements(['security']);
8589
86- $monolog->handler('main')
87- // ...
90+ $monolog->handler('main')
91+ // ...
8892
89- ->channels()->elements(['!security']);
93+ ->channels()->elements(['!security']);
94+ }
9095 };
9196
9297 .. caution ::
@@ -131,13 +136,13 @@ You can also configure additional channels without the need to tag your services
131136
132137 .. code-block :: yaml
133138
134- # config/packages/prod/ monolog.yaml
139+ # config/packages/monolog.yaml
135140 monolog :
136141 channels : ['foo', 'bar', 'foo_bar']
137142
138143 .. code-block :: xml
139144
140- <!-- config/packages/prod/ monolog.xml -->
145+ <!-- config/packages/monolog.xml -->
141146 <container xmlns =" http://symfony.com/schema/dic/services"
142147 xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
143148 xmlns : monolog =" http://symfony.com/schema/dic/monolog"
@@ -155,7 +160,7 @@ You can also configure additional channels without the need to tag your services
155160
156161 .. code-block :: php
157162
158- // config/packages/prod/ monolog.php
163+ // config/packages/monolog.php
159164 use Symfony\Config\MonologConfig;
160165
161166 return static function (MonologConfig $monolog) {
0 commit comments