Define a service with lazy loading logger in symfony

  dependency-injection, interface, lazy-loading, symfony

How to inject in a Symfony service the Logger component in lazy mode using DI. My intention is to log information in some methods. The logger instance must be created at the first time is used.

Something like this:

namespace AppServices;

use PsrLogLoggerInterface;

class XxxService{
    private $logger;

    public function some_method(){
      echo "method without logging"
      return $this;

    private function _log($text){
        if(!$this->logger) $this->logger = new LoggerInterface(); //wrong approach creating object from Interface

    public funtion method_withlog(){
        $this->_log("text logged")
        return $this;

(new XxxService()))
    ->method_withlog() //here is created

Source: Symfony Questions