10000 Merge branch '4.3' into 4.4 · symfony/symfony-docs@4dcd9f8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4dcd9f8

Browse files
committed
Merge branch '4.3' into 4.4
* 4.3: ISSUE-12195: - Added documentation about how to autowire monolog channels.
2 parents b6ecca9 + b344f08 commit 4dcd9f8

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

logging.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@ passed as a service.
349349
was introduced in Symfony 4.2.
350350

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

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

logging/channels_handlers.rst

Lines changed: 23 additions & 3 deletions
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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,8 @@ channel when injecting the logger in a service.
530530
531531
.. tip::
532532

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

537536
.. _dic_tags-monolog-processor:
538537

service_container/3.3-di-changes.rst

Lines changed: 3 additions & 1 deletion
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

Lines changed: 5 additions & 0 deletions
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)
0