8000 minor #12196 [Monolog] Add documentation about how to autowire monolo… · symfony/symfony-docs@2062eea · GitHub
[go: up one dir, main page]

Skip to content

Commit 2062eea

Browse files
committed
minor #12196 [Monolog] Add documentation about how to autowire monolog channels (adrenalinkin)
This PR was merged into the 4.3 branch. Discussion ---------- [Monolog] Add documentation about how to autowire monolog channels resolved: #12195 - Added documentation about how to autowire monolog channels. Relates to [Symfony autowiring monolog channels](symfony/monolog-bundle#315) Commits ------- 2e5a627 ISSUE-12195: - Added documentation about how to autowire monolog channels.
2 parents bca6cf3 + 2e5a627 commit 2062eea

File tree

11 files changed

+89
-2
lines changed

11 files changed

+89
-2
lines changed

bundles/best_practices.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,11 @@ to the service id. For example, in MonologBundle, an alias is created from
463463
``Psr\Log\LoggerInterface`` to ``logger`` so that the ``LoggerInterface`` type-hint
464464
can be used for autowiring.
465465

466+
.. versionadded:: 4.2
467+
468+
Since Monolog Bundle 3.5 each channel bind into container by type-hinted alias.
469+
More info in the part about :ref:`how to autowire monolog channels <monolog-autowire-channels>`.
470+
466471
Services should not use autowiring or autoconfiguration. Instead, all services should
467472
be defined explicitly.
468473

console/commands_as_services.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ the command class will automatically be registered as a service and passed the `
5353
argument (thanks to autowiring). In other words, *just* by creating this class, everything
5454
works! You can call the ``app:sunshine`` command and start logging.
5555

56+
.. versionadded:: 4.2
57+
58+
Since Monolog Bundle 3.5 each channel bind into container by type-hinted alias.
59+
More info in the part about :ref:`how to autowire monolog channels <monolog-autowire-channels>`.
60+
5661
.. caution::
5762

5863
You *do* have access to services in ``configure()``. However, if your command is

controller.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@ the argument by its name:
275275
])
276276
;
277277
278+
.. versionadded:: 4.2
279+
280+
Since Monolog Bundle 3.5 each channel bind into container by type-hinted alias.
281+
More info in the part about :ref:`how to autowire monolog channels <monolog-autowire-channels>`.
282+
278283
Like with all services, you can also use regular :ref:`constructor injection <services-constructor-injection>`
279284
in your controllers.
280285

logging.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,11 @@ specific channel (``app`` by default), use the ``monolog.logger`` tag with the
353353
``channel`` property as explained in the
354354
:ref:`Dependency Injection reference <dic_tags-monolog>`.
355355

356+
.. versionadded:: 4.2
357+
358+
Since Monolog Bundle 3.5 each channel bind into container by type-hinted alias.
359+
More info in the part about :ref:`how to autowire monolog channels <monolog-autowire-channels>`.
360+
356361
Adding extra Data to each Log (e.g. a unique request token)
357362
-----------------------------------------------------------
358363

logging/channels_handlers.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ the channel).
1818
in the container (use the ``php bin/console debug:container monolog`` command
1919
to see a full list) and those are injected into different services.
2020

21+
.. versionadded:: 4.2
22+
23+
Since Monolog Bundle 3.5 each channel bind into container by type-hinted alias.
24+
More info in the part about :ref:`how to autowire monolog channels <monolog-autowire-channels>`.
25+
2126
.. _logging-channel-handler:
2227

2328
Switching a Channel to a different Handler
@@ -172,3 +177,26 @@ Symfony automatically registers one service per channel (in this example, the
172177
channel ``foo`` creates a service called ``monolog.logger.foo``). In order to
173178
inject this service into others, you must update the service configuration to
174179
:ref:`choose the specific service to inject <services-wire-specific-service>`.
180+
181+
.. _monolog-autowire-channels:
182+
183+
How to autowire logger channels
184+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
185+
186+
.. versionadded:: 4.2
187+
188+
This feature available since MonologBundle 3.5
189+
190+
Each channel already bind into container by type-hinted alias.
191+
Just use method variables, which follows next naming template ``Psr\Log\LoggerInterface $<channel>Logger``.
192+
For example, you have ``App\Log\CustomLogger`` service which tagged by ``app`` logger channel
193+
as described in part :ref:`Logging with a custom logging channel <dic_tags-monolog>`.
194+
Now you can remove service configuration at all and change constructor signature:
195+
196+
.. code-block:: diff
197+
198+
- public function __construct(LoggerInterface $logger)
199+
+ public function __construct(LoggerInterface $appLogger)
200+
{
201+
$this->logger = $appLogger;
202+
}

quick_tour/the_architecture.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ But wait! Something *very* cool just happened. Symfony read the ``LoggerInterfac
5252
type-hint and automatically figured out that it should pass us the Logger object!
5353
This is called *autowiring*.
5454

55+
.. versionadded:: 4.2
56+
57+
Furthermore, since Monolog Bundle 3.5 each channel bind into container by type-hinted alias.
58+
More info in the part about :ref:`how to autowire monolog channels <monolog-autowire-channels>`.
59+
5560
Every bit of work that's done in a Symfony app is done by an *object*: the Logger
5661
object logs things and the Twig object renders templates. These objects are called
5762
*services* and they are *tools* that help you build rich features.

reference/configuration/monolog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ The MonologBundle integrates the Monolog :doc:`logging </logging>` library in
88
Symfony applications. All these options are configured under the ``monolog`` key
99
in your application configuration.
1010

11+
.. versionadded:: 4.2
12+
13+
Since Monolog Bundle 3.5 each channel bind into container by type-hinted alias.
14+
More info in the part about :ref:`how to autowire monolog channels <monolog-autowire-channels>`.
15+
1116
.. code-block:: terminal
1217
1318
# displays the default config values defined by Symfony

reference/dic_tags.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,11 @@ channel when injecting the logger in a service.
534534
the corresponding logger service from the service container directly (see
535535
:ref:`monolog-channels-config`).
536536

537+
.. versionadded:: 4.2
538+
539+
Since Monolog Bundle 3.5 each channel bind into container by type-hinted alias.
540+
More info in the part about :ref:`how to autowire monolog channels <monolog-autowire-channels>`.
541+
537542
.. _dic_tags-monolog-processor:
538543

539544
monolog.processor

service_container.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,13 @@ But, you can control this and pass in a different logger:
616616
This tells the container that the ``$logger`` argument to ``__construct`` should use
617617
service whose id is ``monolog.logger.request``.
618618

619+
.. versionadded:: 4.2
620+
621+
Since Monolog Bundle 3.5 each channel already into container by type-hinted alias.
622+
Instead of explicit declare configuration ``$logger: '@monolog.logger.request'``
623+
just use variable in the constructor signature: ``Psr\Log\LoggerInterface $requestLogger``.
624+
More info in the part about :ref:`how to autowire monolog channels <monolog-autowire-channels>`.
625+
619626
.. _container-debug-container:
620627

621628
For a full list of *all* possible services in the container, run:
@@ -714,6 +721,11 @@ argument for *any* service defined in this file! You can bind arguments by name
714721
(e.g. ``$adminEmail``), by type (e.g. ``Psr\Log\LoggerInterface``) or both
715722
(e.g. ``Psr\Log\LoggerInterface $requestLogger``).
716723

724+
.. versionadded:: 4.2
725+
726+
Since Monolog Bundle 3.5 each channel already bind into container by type-hinted alias.
727+
More info in the part about :ref:`how to autowire monolog channels <monolog-autowire-channels>`.
728+
717729
The ``bind`` config can also be applied to specific services or when loading many
718730
services at once (i.e. :ref:`service-psr4-loader`).
719731

service_container/3.3-di-changes.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,15 @@ create the class::
392392
}
393393

394394
Great! In Symfony 3.2 or lower, you would now need to register this as a service
395-
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>`.
395+
in ``services.yaml`` and tag it with ``kernel.event_subscriber``.
396+
In Symfony 3.3, you're already done!
397+
398+
.. versionadded:: 4.2
399+
400+
Furthermore, since Monolog Bundle 3.5 each channel bind into container by type-hinted alias.
401+
More info in the part about :ref:`how to autowire monolog channels <monolog-autowire-channels>`.
402+
403+
The service is :ref:`automatically registered <service-33-changes-automatic-registration>`.
397404
And thanks to ``autoconfigure``, Symfony automatically tags the service because
398405
it implements ``EventSubscriberInterface``.
399406

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