@@ -23,27 +23,27 @@ Switching a Channel to a different Handler
23
23
Now, suppose you want to log the ``security `` channel to a different file.
24
24
To do this, create a new handler and configure it to log only messages
25
25
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 >`:
28
27
29
28
.. configuration-block ::
30
29
31
30
.. code-block :: yaml
32
31
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']
47
47
48
48
.. code-block :: xml
49
49
@@ -56,12 +56,14 @@ can do it in any (or all) environments:
56
56
http://symfony.com/schema/dic/monolog
57
57
https://symfony.com/schema/dic/monolog/monolog-1.0.xsd" >
58
58
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 >
65
67
66
68
<monolog : handler name =" main" type =" stream" path =" %kernel.logs_dir%/main.log" >
67
69
<!-- ... -->
@@ -75,18 +77,21 @@ can do it in any (or all) environments:
75
77
.. code-block :: php
76
78
77
79
// config/packages/prod/monolog.php
80
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
78
81
use Symfony\Config\MonologConfig;
79
82
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']);
85
89
86
- $monolog->handler('main')
87
- // ...
90
+ $monolog->handler('main')
91
+ // ...
88
92
89
- ->channels()->elements(['!security']);
93
+ ->channels()->elements(['!security']);
94
+ }
90
95
};
91
96
92
97
.. caution ::
@@ -131,13 +136,13 @@ You can also configure additional channels without the need to tag your services
131
136
132
137
.. code-block :: yaml
133
138
134
- # config/packages/prod/ monolog.yaml
139
+ # config/packages/monolog.yaml
135
140
monolog :
136
141
channels : ['foo', 'bar', 'foo_bar']
137
142
138
143
.. code-block :: xml
139
144
140
- <!-- config/packages/prod/ monolog.xml -->
145
+ <!-- config/packages/monolog.xml -->
141
146
<container xmlns =" http://symfony.com/schema/dic/services"
142
67F4
147
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
143
148
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
155
160
156
161
.. code-block :: php
157
162
158
- // config/packages/prod/ monolog.php
163
+ // config/packages/monolog.php
159
164
use Symfony\Config\MonologConfig;
160
165
161
166
return static function (MonologConfig $monolog) {
0 commit comments