Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 Injecting logger service

I got a strange problem using the logger service in symfony 2:

When injecting the logger to a service, I get a type error because LoggerInterface expected but Symfony\Bridge\Monolog\Logger given.

Also if I try to inject a custom logger, I get error because of undefined service.

Here is my code:

confiy.yml

monolog:
channels: ['payment']
handlers:
    paymentlog:
        type: stream
        path: "%kernel.logs_dir%/payment.log"
        level: debug
        channels: [payment]

service.yml

#payment_services
  payment.gateway_payments:
    class: AppBundle\Service\paymentService
    arguments: ["@service_container", "@doctrine.orm.entity_manager", "@logger"]

Service:

<?php

  namespace AppBundle\Service;

  use Symfony\Component\DependencyInjection\ContainerInterface;
  use Doctrine\ORM\EntityManager;
  use Symfony\Component\HttpKernel\Log\LoggerInterface;

  class paymentService {

    private $container;
    private $em;
    private $logger;

    public function __construct(ContainerInterface $container, EntityManager $em, LoggerInterface $logger){
    $this->container = $container;
    $this->em = $em;
    $this->logger = $logger;
}

Also injecting the logger with @monolog.logger.paymentlog is giving me an error "undefinded service"

Can someone please tell me where I am wrong?

THX a lot.

like image 284
user1827297 Avatar asked Aug 01 '17 15:08

user1827297


1 Answers

try this:

use Monolog\Logger;

instead of this:

use Symfony\Component\HttpKernel\Log\LoggerInterface;

And after this;

public function __construct(ContainerInterface $container, EntityManager $em, Logger $logger){

insetad of this:

public function __construct(ContainerInterface $container, EntityManager $em, LoggerInterface $logger){
like image 93
Alessandro Minoccheri Avatar answered Oct 20 '22 00:10

Alessandro Minoccheri