44How to Log Messages to different Files
55======================================
66
7- The Symfony Standard Edition contains a bunch of channels for logging: ``doctrine ``,
8- ``event ``, ``security `` and ``request ``. Each channel corresponds to a logger
9- service (``monolog.logger.XXX ``) in the container and is injected to the
10- concerned service. The purpose of channels is to be able to organize different
11- types of log messages.
7+ The Symfony Framework organizes log messages into channels. By default, there
8+ are several channels, including ``doctrine ``, ``event ``, ``security ``, ``request ``
9+ and more. The channel is printed in the log message and can also be used
10+ to direct different channels to different places/files.
1211
1312By default, Symfony logs every message into a single file (regardless of
1413the channel).
1514
15+ .. note ::
16+
17+ Each channel corresponds to a logger service (``monolog.logger.XXX ``)
18+ in the container (use the ``container:debug `` command to see a full list)
19+ and those are injected into different services.
20+
21+ .. _logging-channel-handler :
22+
1623Switching a Channel to a different Handler
1724------------------------------------------
1825
19- Now, suppose you want to log the ``doctrine `` channel to a different file.
20-
21- To do so, just create a new handler and configure it like this :
26+ Now, suppose you want to log the ``security `` channel to a different file
27+ in the `` prod `` environment. To do this, just create a new handler and configure
28+ it to log only messages from the `` security `` channel :
2229
2330.. configuration-block ::
2431
2532 .. code-block :: yaml
2633
27- # app/config/config .yml
34+ # app/config/config_prod .yml
2835 monolog :
2936 handlers :
30- main :
37+ security :
38+ # log all messages (since debug is the lowest level)
39+ level : debug
3140 type : stream
32- path : /var/log/symfony.log
33- channels : ["!doctrine"]
34- doctrine :
35- type : stream
36- path : /var/log/doctrine.log
37- channels : [doctrine]
41+ path : " %kernel.logs_dir%/security.log"
42+ channels : [security]
43+
44+ # an example of *not* logging security channel messages
45+ main :
46+ # ...
47+ # channels: ["!security"]
3848
3949 .. code-block :: xml
4050
@@ -48,16 +58,18 @@ To do so, just create a new handler and configure it like this:
4858 http://symfony.com/schema/dic/monolog/monolog-1.0.xsd"
4959 >
5060 <monolog : config >
51- <monolog : handler name =" main " type =" stream" path =" /var/log/symfony .log" >
61+ <monolog : handler name =" security " type =" stream" path =" %kernel.logs_dir%/security .log" >
5262 <monolog : channels >
53- <monolog : channel >!doctrine </monolog : channel >
63+ <monolog : channel >security </monolog : channel >
5464 </monolog : channels >
5565 </monolog : handler >
5666
57- <monolog : handler name =" doctrine" type =" stream" path =" /var/log/doctrine.log" >
67+ <monolog : handler name =" main" type =" stream" path =" %kernel.logs_dir%/security.log" >
68+ <!--
5869 <monolog:channels>
59- <monolog : channel >doctrine </monolog : channel >
70+ <monolog:channel>!security </monolog:channel>
6071 </monolog:channels>
72+ -->
6173 </monolog : handler >
6274 </monolog : config >
6375 </container >
@@ -67,19 +79,21 @@ To do so, just create a new handler and configure it like this:
6779 // app/config/config.php
6880 $container->loadFromExtension('monolog', array(
6981 'handlers' => array(
70- 'main' => array(
82+ 'security' => array(
7183 'type' => 'stream',
72- 'path' => '/var/log/symfony .log',
84+ 'path' => '%kernel.logs_dir%/security .log',
7385 'channels' => array(
74- '!doctrine ',
86+ 'security ',
7587 ),
7688 ),
77- 'doctrine' => array(
89+ 'main' => array(
7890 'type' => 'stream',
79- 'path' => '/var/log/doctrine.log',
91+ 'path' => '%kernel.logs_dir%/security.log',
92+ /*
8093 'channels' => array(
81- 'doctrine ',
94+ '!security ',
8295 ),
96+ */
8397 ),
8498 ),
8599 ));
0 commit comments