我在symfony 2中使用记录器服务时遇到了一个奇怪的问题:
当将记录器注入服务时,我得到一个类型错误,因为LoggerInterface是预期的,但是给出了Symfony \ Bridge \ Monolog \ Logger。
此外,如果我尝试注入自定义记录器,由于未定义的服务,我会收到错误。
这是我的代码:
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"]
服务:
<?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注入记录器会给我一个错误“未完成的服务”
有人可以告诉我哪里错了吗?
太多了。
答案 0 :(得分:5)
试试这个:
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){