Symfony 2 注入(inject)记录器服务

标签 symfony logging service monolog

我在 symfony 2 中使用记录器服务时遇到了一个奇怪的问题:

将记录器注入(inject)服务时,我收到类型错误,因为 LoggerInterface 预期但 Symfony\Bridge\Monolog\Logger 给出。

此外,如果我尝试注入(inject)自定义记录器,我会因为未定义的服务而出错。

这是我的代码:

配置文件

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

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

服务:
<?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;
}

使用@monolog.logger.paymentlog 注入(inject)记录器也会给我一个错误“未定义的服务”

有人可以告诉我我错在哪里吗?

多谢。

最佳答案

试试这个:

use Monolog\Logger;

而不是这个:
use Symfony\Component\HttpKernel\Log\LoggerInterface;

在此之后;
public function __construct(ContainerInterface $container, EntityManager $em, Logger $logger){

插入此:
public function __construct(ContainerInterface $container, EntityManager $em, LoggerInterface $logger){

关于Symfony 2 注入(inject)记录器服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45442002/

相关文章:

symfony - 学说实体代理异常

php - Symfony 4 - KnpPaginator Bundle "service not found, even though it exists in app' s 容器”

java - 当依赖项使用不同的日志记录时会发生什么?

api - 如何使用服务与 Panoramio API 交互?

mysql - Symfony Doctrine 将相关实体加入鉴别器列

php - 在 Symfony 2 中加载父类

logging - 如何设置 uWSGI 日志级别?

Java 自定义记录器 : logging standards or/and best practices

windows - 没有窗口的 SDL(Windows 服务)

linux - monit:无法识别的服务