Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Monolog channel for some controllers Symfony 3.4 or 4+

I want to change Monolog channel. My declarations works with some classes but never with controllers.

Here is my new admin channel declaration:

#config/packages/dev/monolog.yaml
monolog:
    handlers:
        admin:
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%-admin.log"
            level: debug
            channels: ["admin"]

I use it successfuly with my Authenticator by adding a tag:

#config/services.yaml
# The form guard authenticator for the admin access
app.security.admin_authenticator:
    class: App\Security\AdminAuthenticator
    autowire: true
    tags:
        - { name: monolog.logger, channel: admin}

The last line of services.yaml file do the job, my Authenticator is no more logging in app channel, it's logging in admin channel.

Now, I want to use this channel with the controllers under the Admin subdirectory, so I add a similar tag in my declaration:

#config/services.yaml
App\Controller\Admin\:
    resource: '../src/Controller/Admin'
    tags:
        - 'controller.service_arguments'
        - { name: monolog.logger, channel: admin}

But it seems there is no impact. I am still logging in app channel. (I did some verification, like refresh the cache). I don't find my error.

like image 312
Alexandre Tranchant Avatar asked Mar 11 '18 11:03

Alexandre Tranchant


People also ask

What is monolog channel?

In simple words, you can define a channel as a separate log file. Generally you might need to classify your log files for different services or modules. For this purpose, Monolog allows you to create different channels, where each can log separately to different file and allow you to configure your log per channel.

What is monolog Symfony?

Monolog is the existing standard logging library for PHP. It is most popular in PHP frameworks such as Laravel and Symfony, where it implements a common interface for logging libraries. This article talks about the step-by-step process of using PHP Monolog in your application.

Where are Symfony logs?

By default, Symfony logs are stored in var/log/dev. log and var/log/prod. log within the project directory, depending on the environment, but these defaults can be changed in the Monolog package configuration file found at config/packages/monolog. php.


2 Answers

I cannot figure it out with the tag method. However, you can define your monolog channel as argument of your controller constructor:

# monolog package configuration
monolog:
    channels: [ 'admin' ]
    handlers:
        admin:
            type: stream
            path: "%kernel.logs_dir%/%kernel.environment%-admin.log"
            level: debug
            channels: ["admin"]

Auto-wiring configuration:

#config/services.yaml
App\Controller\Admin\:
    resource: '../src/Controller/Admin'
    arguments:
        $logger: '@monolog.logger.admin'
    tags:
        - 'controller.service_arguments'

And in your controllers

/**
 * SecurityController constructor.
 * @param LoggerInterface $logger
 */
public function __construct(LoggerInterface $logger)
{
    $this->logger = $logger;
}
like image 97
MatMouth Avatar answered Oct 14 '22 14:10

MatMouth


I haven't tried this but it most probably because your controller doesn't implement LoggerAwareInterface thus the logger tag has no impact.

Check which interface AdminAuthenticator is implementing (to see how the logger is being set) then do the same for the controller it should work.

#services.yaml
App\Controller\Admin\:
    resource: '../src/Controller/Admin'
    calls:
        - [ setLogger, [ '@logger' ] ]
    tags:
        - 'controller.service_arguments'
        - { name: monolog.logger, channel: admin}
like image 44
mrbm Avatar answered Oct 14 '22 13:10

mrbm