Skip to content

Commit 864f6cc

Browse files
committed
Merge branch '4.4'
* 4.4: ISSUE-12195: - Added documentation about how to autowire monolog channels.
2 parents c468d8a + 4dcd9f8 commit 864f6cc

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

logging.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ receive a call to its method ``setLogger()`` with the default logger service
344344
passed as a service.
345345

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

351351
Adding extra Data to each Log (e.g. a unique request token)

logging/channels_handlers.rst

+23-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ the channel).
1414

1515
.. note::
1616

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

2121
.. _logging-channel-handler:
2222

@@ -172,3 +172,23 @@ Symfony automatically registers one service per channel (in this example, the
172172
channel ``foo`` creates a service called ``monolog.logger.foo``). In order to
173173
inject this service into others, you must update the service configuration to
174174
:ref:`choose the specific service to inject <services-wire-specific-service>`.
175+
176+
.. _monolog-autowire-channels:
177+
178+
How to Autowire Logger Channels
179+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
180+
181+
Starting from `MonologBundle`_ 3.5 you can autowire different Monolog channels
182+
by type-hinting your service arguments with the following syntax:
183+
``Psr\Log\LoggerInterface $<channel>Logger``. For example, to inject the service
184+
related to the ``app`` logger channel use this:
185+
186+
.. code-block:: diff
187+
188+
- public function __construct(LoggerInterface $logger)
189+
+ public function __construct(LoggerInterface $appLogger)
190+
{
191+
$this->logger = $appLogger;
192+
}
193+
194+
.. _`MonologBundle`: https://github.com/symfony/monolog-bundle

reference/dic_tags.rst

+2-3
Original file line numberDiff line numberDiff line change
@@ -525,9 +525,8 @@ channel when injecting the logger in a service.
525525
526526
.. tip::
527527

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

532531
.. _dic_tags-monolog-processor:
533532

service_container/3.3-di-changes.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,9 @@ create the class::
393393

394394
Great! In Symfony 3.2 or lower, you would now need to register this as a service
395395
in ``services.yaml`` and tag it with ``kernel.event_subscriber``. In Symfony 3.3,
396-
you're already done! The service is :ref:`automatically registered <service-33-changes-automatic-registration>`.
396+
you're already done!
397+
398+
The service is :ref:`automatically registered <service-33-changes-automatic-registration>`.
397399
And thanks to ``autoconfigure``, Symfony automatically tags the service because
398400
it implements ``EventSubscriberInterface``.
399401

service_container/autowiring.rst

+5
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ class is type-hinted.
248248
adds an alias: ``Psr\Log\LoggerInterface`` that points to the ``logger`` service.
249249
This is why arguments type-hinted with ``Psr\Log\LoggerInterface`` can be autowired.
250250

251+
.. versionadded:: 4.2
252+
253+
Since Monolog Bundle 3.5 each channel bind into container by type-hinted alias.
254+
More info in the part about :ref:`how to autowire monolog channels <monolog-autowire-channels>`.
255+
251256
.. _autowiring-interface-alias:
252257

253258
Working with Interfaces

0 commit comments

Comments
 (0)