Skip to content

Commit 9fb296d

Browse files
committed
feature #4604 Making the channel handler more useful by showing it on the prod environment (weaverryan)
This PR was merged into the 2.3 branch. Discussion ---------- Making the channel handler more useful by showing it on the prod environment | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | 2.3+ | Fixed tickets | n/a Hi guys! I realized there were a few practical problems with this: 1) Normally, there is no `monolog` configuration in `config.yml` - so it's odd to show an example there 2) In the `prod` environment (by default, unless you set doctrine's logging explicitly to true), the `doctrine` channel is not logged at all. So, if you tried this example in `config_prod.yml`, it probably wouldn't work. But security is always there. Thanks! P.S. After merging to 2.6, `container:debug` should be changed to `debug:container`. Commits ------- bc79b21 Adding one more note about why we're in config.yml 78323d8 Changing back to config.yml and fixing some code block mistakes thanks to Wouter f9f3c3f Making the channel handler more useful by showing it on the prod environment
2 parents 7e7020d + bc79b21 commit 9fb296d

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

Diff for: cookbook/logging/channels_handlers.rst

+37-26
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,29 @@
44
How 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

1312
By default, Symfony logs every message into a single file (regardless of
1413
the 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+
1623
Switching 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+
To do this, just create a new handler and configure it to log only messages
28+
from the ``security`` channel. You might add this in ``config.yml`` to log
29+
in all environments, or just ``config_prod.yml`` to happen only in ``prod``:
2230

2331
.. configuration-block::
2432

@@ -27,14 +35,17 @@ To do so, just create a new handler and configure it like this:
2735
# app/config/config.yml
2836
monolog:
2937
handlers:
30-
main:
31-
type: stream
32-
path: /var/log/symfony.log
33-
channels: ["!doctrine"]
34-
doctrine:
38+
security:
39+
# log all messages (since debug is the lowest level)
40+
level: debug
3541
type: stream
36-
path: /var/log/doctrine.log
37-
channels: [doctrine]
42+
path: "%kernel.logs_dir%/security.log"
43+
channels: [security]
44+
45+
# an example of *not* logging security channel messages for this handler
46+
main:
47+
# ...
48+
# channels: ["!security"]
3849
3950
.. code-block:: xml
4051
@@ -48,15 +59,16 @@ To do so, just create a new handler and configure it like this:
4859
http://symfony.com/schema/dic/monolog/monolog-1.0.xsd"
4960
>
5061
<monolog:config>
51-
<monolog:handler name="main" type="stream" path="/var/log/symfony.log">
62+
<monolog:handler name="security" type="stream" path="%kernel.logs_dir%/security.log">
5263
<monolog:channels>
53-
<monolog:channel>!doctrine</monolog:channel>
64+
<monolog:channel>security</monolog:channel>
5465
</monolog:channels>
5566
</monolog:handler>
5667
57-
<monolog:handler name="doctrine" type="stream" path="/var/log/doctrine.log">
68+
<monolog:handler name="main" type="stream" path="%kernel.logs_dir%/main.log">
69+
<!-- ... -->
5870
<monolog:channels>
59-
<monolog:channel>doctrine</monolog:channel>
71+
<monolog:channel>!security</monolog:channel>
6072
</monolog:channels>
6173
</monolog:handler>
6274
</monolog:config>
@@ -67,18 +79,17 @@ 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(
78-
'type' => 'stream',
79-
'path' => '/var/log/doctrine.log',
89+
'main' => array(
90+
// ...
8091
'channels' => array(
81-
'doctrine',
92+
'!security',
8293
),
8394
),
8495
),

0 commit comments

Comments
 (0)