@@ -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,15 @@ 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
+ </monolog : config >
67
+ </when >
65
68
66
69
<monolog : handler name =" main" type =" stream" path =" %kernel.logs_dir%/main.log" >
67
70
<!-- ... -->
@@ -75,18 +78,21 @@ can do it in any (or all) environments:
75
78
.. code-block :: php
76
79
77
80
// config/packages/prod/monolog.php
81
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
78
82
use Symfony\Config\MonologConfig;
79
83
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']);
85
90
86
- $monolog->handler('main')
87
- // ...
91
+ $monolog->handler('main')
92
+ // ...
88
93
89
- ->channels()->elements(['!security']);
94
+ ->channels()->elements(['!security']);
95
+ }
90
96
};
91
97
92
98
.. caution ::
@@ -131,13 +137,13 @@ You can also configure additional channels without the need to tag your services
131
137
132
138
.. code-block :: yaml
133
139
134
- # config/packages/prod/ monolog.yaml
140
+ # config/packages/monolog.yaml
135
141
monolog :
136
142
channels : ['foo', 'bar', 'foo_bar']
137
143
138
144
.. code-block :: xml
139
145
140
- <!-- config/packages/prod/ monolog.xml -->
146
+ <!-- config/packages/monolog.xml -->
141
147
<container xmlns =" http://symfony.com/schema/dic/services"
142
148
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
143
149
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
155
161
156
162
.. code-block :: php
157
163
158
- // config/packages/prod/ monolog.php
164
+ // config/packages/monolog.php
159
165
use Symfony\Config\MonologConfig;
160
166
161
167
return static function (MonologConfig $monolog): void {
0 commit comments