Skip to content

Commit

Permalink
Merge branch '4.4'
Browse files Browse the repository at this point in the history
* 4.4:
  ISSUE-12195: - Added documentation about how to autowire monolog channels.
  • Loading branch information
javiereguiluz committed Sep 26, 2019
2 parents c468d8a + 4dcd9f8 commit 864f6cc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
4 changes: 2 additions & 2 deletions logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,8 @@ receive a call to its method ``setLogger()`` with the default logger service
passed as a service.

If you want to use in your own services a pre-configured logger which uses a
specific channel (``app`` by default), use the ``monolog.logger`` tag with the
``channel`` property as explained in the
specific channel (``app`` by default), you can either :ref:`autowire monolog channels <monolog-autowire-channels>`
or use the ``monolog.logger`` tag with the ``channel`` property as explained in the
:ref:`Dependency Injection reference <dic_tags-monolog>`.

Adding extra Data to each Log (e.g. a unique request token)
Expand Down
26 changes: 23 additions & 3 deletions logging/channels_handlers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ the channel).

.. note::

Each channel corresponds to a logger service (``monolog.logger.XXX``)
in the container (use the ``php bin/console debug:container monolog`` command
to see a full list) and those are injected into different services.
Each channel corresponds to a different logger service (``monolog.logger.XXX``)
Use the ``php bin/console debug:container monolog`` command to see a full
list of services and learn :ref:`how to autowire monolog channels <monolog-autowire-channels>`.

.. _logging-channel-handler:

Expand Down Expand Up @@ -172,3 +172,23 @@ Symfony automatically registers one service per channel (in this example, the
channel ``foo`` creates a service called ``monolog.logger.foo``). In order to
inject this service into others, you must update the service configuration to
:ref:`choose the specific service to inject <services-wire-specific-service>`.

.. _monolog-autowire-channels:

How to Autowire Logger Channels
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Starting from `MonologBundle`_ 3.5 you can autowire different Monolog channels
by type-hinting your service arguments with the following syntax:
``Psr\Log\LoggerInterface $<channel>Logger``. For example, to inject the service
related to the ``app`` logger channel use this:

.. code-block:: diff
- public function __construct(LoggerInterface $logger)
+ public function __construct(LoggerInterface $appLogger)
{
$this->logger = $appLogger;
}
.. _`MonologBundle`: https://github.com/symfony/monolog-bundle
5 changes: 2 additions & 3 deletions reference/dic_tags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,8 @@ channel when injecting the logger in a service.
.. tip::

You can also configure custom channels in the configuration and retrieve
the corresponding logger service from the service container directly (see
:ref:`monolog-channels-config`).
You can create :doc:`custom channels </logging/channels_handlers>` and
even :ref:`autowire logging channels <monolog-autowire-channels>`.

.. _dic_tags-monolog-processor:

Expand Down
4 changes: 3 additions & 1 deletion service_container/3.3-di-changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ create the class::

Great! In Symfony 3.2 or lower, you would now need to register this as a service
in ``services.yaml`` and tag it with ``kernel.event_subscriber``. In Symfony 3.3,
you're already done! The service is :ref:`automatically registered <service-33-changes-automatic-registration>`.
you're already done!

The service is :ref:`automatically registered <service-33-changes-automatic-registration>`.
And thanks to ``autoconfigure``, Symfony automatically tags the service because
it implements ``EventSubscriberInterface``.

Expand Down
5 changes: 5 additions & 0 deletions service_container/autowiring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ class is type-hinted.
adds an alias: ``Psr\Log\LoggerInterface`` that points to the ``logger`` service.
This is why arguments type-hinted with ``Psr\Log\LoggerInterface`` can be autowired.

.. versionadded:: 4.2

Since Monolog Bundle 3.5 each channel bind into container by type-hinted alias.
More info in the part about :ref:`how to autowire monolog channels <monolog-autowire-channels>`.

.. _autowiring-interface-alias:

Working with Interfaces
Expand Down

0 comments on commit 864f6cc

Please sign in to comment.