monolog custom service access from a service

425 views Asked by At

Please, Is there a quick way to have monolog service available in a an_other_service service without passing the monolog service reference as argument from a controller ?

Exactly I've created a custom monolog channel which is written to a specific log file. Normally in a controller I get my monolog custom service with

$this->logger = $this->container->get('monolog.logger.test');

For now I've got my custom log service passing the instance of the logger as an argument when I call a method of an_other_service service. Being in an_other_service service Is there a clear way to get access to a custom monolog ? and to the normal monolog service ?

1

There are 1 answers

2
goto On BEST ANSWER

inject the service in your service.

class ServiceCustom 
{
    private $logger;

    public function setLogger(YourLoggerClass $logger)
    {
        $this->logger= $logger;
    }

    // ...
}


services:
    service_custom:
        class:     Namespace/ServiceCustom
        arguments: []
        calls:
            - [setLogger, ["@monolog.logger.test"]]

Documentation