1

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 Answers1

1

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

goto
  • 7,908
  • 10
  • 48
  • 58
  • I thank you for your answer but this is not possible in my case because I am trying to use monolog in an existent project where there are predefined classes and services. It becomes too much complex to add such a row in constructor of classes that extend other classes that extend other …and I don't have clear all the service definition part of that project code. – Artemide Innominato Jun 18 '15 at 10:50
  • if it fits your needs, you can use a simple method to inject the service, see my edit – goto Jun 18 '15 at 12:03