Symfony create a custom logger file and understand behind the scene
Aug 9, 2023 Symfony
Sometimes we want to log specific data on specific log files, to do so add the following lines in your monolog configuration (https://symfony.com/doc/current/logging/channels_handlers.html) :
monolog: |
Here we are creating a custom handler with a custom channel named “my_channel”
Then we can use it on our controller or service like below :
private LoggerInterface $logger; |
To take advantage of autowiring (available since version 3.5 of monolog bundle) the injected service must be named <custom_channel_name>Logger => myChannelLogger.
Let us try to understand where is this naming rule inside the vendors
Go to this file : vendor/symfony/monolog-bundle/DependencyInjection/Compiler/LoggerChannelPass.php on line 86
I have commented the code below so we understand what is happening :
foreach ($container->getParameter('monolog.additional_channels') as $chan) { |
Let us see what is happening inside the createLogger method : see comments inside the code
protected function createLogger($channel, $loggerId, ContainerBuilder $container) //we instantiate the logger service |
symfony
php
Categories
Recent posts
Create a custom attribute/annotation and understand @security annotation behind the scene [Symfony]
Create a ssl certificate using let's encrypt [System]
Usual Makefile commands on symfony project [Symfony]
Symfony create a custom logger file and understand behind the scene [Symfony]
Démarrer un nouveau projet symfony 6 comme un sénior sous docker [Symfony]