5

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.

user1827297
  • 83
  • 1
  • 3
  • What do you want? Are you want custom logger for payment.log? – Imanali Mamadiev Aug 01 '17 at 16:01
  • 1
    Read the [documentation](http://symfony.com/doc/current/reference/dic_tags.html#dic-tags-monolog). Also: [This](http://symfony.com/doc/current/logging.html#using-a-logger-inside-a-service). – ccKep Aug 01 '17 at 21:10
  • @ccKep: Thank you for this hint, after fixing the problem with my namespace this did the trick. And Yes I wanted to have a custom logger for payment log, but couldn't use a seperate channel for it, since it would give me errors. – user1827297 Aug 03 '17 at 07:25

1 Answers1

7

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){
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
  • 1
    Close but no cigar. Still want to typehint aganst LoggerInterface. They just need to use the correct namespace. They copy/pasted an old piece of code. – Cerad Aug 01 '17 at 15:43
  • Yes maybe you are right @Cerad I haven't thought about the IDE is generating the wrong use statement. – Alessandro Minoccheri Aug 01 '17 at 15:44
  • I mean normally yes unless the OP is on an earlier Symfony version – Jason Roman Aug 01 '17 at 18:17
  • If they were using an old enough version then they would not have had the problem in the first place. And no, the namespace you posted is not correct. – Cerad Aug 01 '17 at 18:29
  • 1
    Woops I don't know where the 7 came from... `Psr\Log\LoggerInterface` – Jason Roman Aug 02 '17 at 03:01
  • Thank you very much @Cerad and Jason Roman. With your help now everything works fine. Reading the doc I did not see the use-Statement so I did a wild guess. Feel stupid now... – user1827297 Aug 03 '17 at 07:28