4
4
How to Log Messages to different Files
5
5
======================================
6
6
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.
12
11
13
12
By default, Symfony logs every message into a single file (regardless of
14
13
the channel).
15
14
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
+
16
23
Switching a Channel to a different Handler
17
24
------------------------------------------
18
25
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 :
22
29
23
30
.. configuration-block ::
24
31
25
32
.. code-block :: yaml
26
33
27
- # app/config/config .yml
34
+ # app/config/config_prod .yml
28
35
monolog :
29
36
handlers :
30
- main :
37
+ security :
38
+ # log all messages (since debug is the lowest level)
39
+ level : debug
31
40
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"]
38
48
39
49
.. code-block :: xml
40
50
@@ -48,16 +58,18 @@ To do so, just create a new handler and configure it like this:
48
58
http://symfony.com/schema/dic/monolog/monolog-1.0.xsd"
49
59
>
50
60
<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" >
52
62
<monolog : channels >
53
- <monolog : channel >!doctrine </monolog : channel >
63
+ <monolog : channel >security </monolog : channel >
54
64
</monolog : channels >
55
65
</monolog : handler >
56
66
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
+ <!--
58
69
<monolog:channels>
59
- <monolog : channel >doctrine </monolog : channel >
70
+ <monolog:channel>!security </monolog:channel>
60
71
</monolog:channels>
72
+ -->
61
73
</monolog : handler >
62
74
</monolog : config >
63
75
</container >
@@ -67,19 +79,21 @@ To do so, just create a new handler and configure it like this:
67
79
// app/config/config.php
68
80
$container->loadFromExtension('monolog', array(
69
81
'handlers' => array(
70
- 'main' => array(
82
+ 'security' => array(
71
83
'type' => 'stream',
72
- 'path' => '/var/log/symfony .log',
84
+ 'path' => '%kernel.logs_dir%/security .log',
73
85
'channels' => array(
74
- '!doctrine ',
86
+ 'security ',
75
87
),
76
88
),
77
- 'doctrine' => array(
89
+ 'main' => array(
78
90
'type' => 'stream',
79
- 'path' => '/var/log/doctrine.log',
91
+ 'path' => '%kernel.logs_dir%/security.log',
92
+ /*
80
93
'channels' => array(
81
- 'doctrine ',
94
+ '!security ',
82
95
),
96
+ */
83
97
),
84
98
),
85
99
));
0 commit comments