-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Making the channel handler more useful by showing it on the prod environment #4604
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,21 +4,29 @@ | |
How to Log Messages to different Files | ||
====================================== | ||
|
||
The Symfony Standard Edition contains a bunch of channels for logging: ``doctrine``, | ||
``event``, ``security`` and ``request``. Each channel corresponds to a logger | ||
service (``monolog.logger.XXX``) in the container and is injected to the | ||
concerned service. The purpose of channels is to be able to organize different | ||
types of log messages. | ||
The Symfony Framework organizes log messages into channels. By default, there | ||
are several channels, including ``doctrine``, ``event``, ``security``, ``request`` | ||
and more. The channel is printed in the log message and can also be used | ||
to direct different channels to different places/files. | ||
|
||
By default, Symfony logs every message into a single file (regardless of | ||
the channel). | ||
|
||
.. note:: | ||
|
||
Each channel corresponds to a logger service (``monolog.logger.XXX``) | ||
in the container (use the ``container:debug`` command to see a full list) | ||
and those are injected into different services. | ||
|
||
.. _logging-channel-handler: | ||
|
||
Switching a Channel to a different Handler | ||
------------------------------------------ | ||
|
||
Now, suppose you want to log the ``doctrine`` channel to a different file. | ||
|
||
To do so, just create a new handler and configure it like this: | ||
Now, suppose you want to log the ``security`` channel to a different file. | ||
To do this, just create a new handler and configure it to log only messages | ||
from the ``security`` channel. You might add this in ``config.yml`` to log | ||
in all environments, or just ``config_prod.yml`` to happen only in ``prod``: | ||
|
||
.. configuration-block:: | ||
|
||
|
@@ -27,14 +35,17 @@ To do so, just create a new handler and configure it like this: | |
# app/config/config.yml | ||
monolog: | ||
handlers: | ||
main: | ||
type: stream | ||
path: /var/log/symfony.log | ||
channels: ["!doctrine"] | ||
doctrine: | ||
security: | ||
# log all messages (since debug is the lowest level) | ||
level: debug | ||
type: stream | ||
path: /var/log/doctrine.log | ||
channels: [doctrine] | ||
path: "%kernel.logs_dir%/security.log" | ||
channels: [security] | ||
|
||
# an example of *not* logging security channel messages for this handler | ||
main: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why didn't you add a comment for this line? |
||
# ... | ||
# channels: ["!security"] | ||
|
||
.. code-block:: xml | ||
|
||
|
@@ -48,15 +59,16 @@ To do so, just create a new handler and configure it like this: | |
http://symfony.com/schema/dic/monolog/monolog-1.0.xsd" | ||
> | ||
<monolog:config> | ||
<monolog:handler name="main" type="stream" path="/var/log/symfony.log"> | ||
<monolog:handler name="security" type="stream" path="%kernel.logs_dir%/security.log"> | ||
<monolog:channels> | ||
<monolog:channel>!doctrine</monolog:channel> | ||
<monolog:channel>security</monolog:channel> | ||
</monolog:channels> | ||
</monolog:handler> | ||
|
||
<monolog:handler name="doctrine" type="stream" path="/var/log/doctrine.log"> | ||
<monolog:handler name="main" type="stream" path="%kernel.logs_dir%/main.log"> | ||
<!-- ... --> | ||
<monolog:channels> | ||
<monolog:channel>doctrine</monolog:channel> | ||
<monolog:channel>!security</monolog:channel> | ||
</monolog:channels> | ||
</monolog:handler> | ||
</monolog:config> | ||
|
@@ -67,18 +79,17 @@ To do so, just create a new handler and configure it like this: | |
// app/config/config.php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs file name update too (I couldn't comment on the XML filename, but that should be changed as well) |
||
$container->loadFromExtension('monolog', array( | ||
'handlers' => array( | ||
'main' => array( | ||
'security' => array( | ||
'type' => 'stream', | ||
'path' => '/var/log/symfony.log', | ||
'path' => '%kernel.logs_dir%/security.log', | ||
'channels' => array( | ||
'!doctrine', | ||
'security', | ||
), | ||
), | ||
'doctrine' => array( | ||
'type' => 'stream', | ||
'path' => '/var/log/doctrine.log', | ||
'main' => array( | ||
// ... | ||
'channels' => array( | ||
'doctrine', | ||
'!security', | ||
), | ||
), | ||
), | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
however, when defining your services, the way to ask for a given channel is not to use
monolog.logger.XXX
directly. It is to inject thelogger
service and set the appropriate tag on your service.this is necessary because the compiler pass reading these tags is also the place creating these services.
I'm not sure documenting the
monolog.logger.XXX
naming convention is actually worth it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 for documenting it as an monolog implementation detail. we should warn to rely on this naming convention
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I think the specific
monolog.logger.XXX
service names are guaranteed in a sense, due to this feature: http://symfony.com/doc/current/cookbook/logging/channels_handlers.html#configure-additional-channels-without-tagged-services. And for me, this has always seems a lot more clear - using a tag to change how a logger is injected (in reality, which logger is injected) always seemed obtuse to me. I wonder if we should tell end-users to use the specific services, and leave the tag-usage primarily for third-party code (where you can't create a channel via config).