Symfony的2注射记录器服务
问题描述:
我在symfony中2使用记录器服务一个奇怪的问题:Symfony的2注射记录器服务
当注射记录到服务,我得到一个类型错误,因为LoggerInterface预期,但Symfony的\桥\独白\记录仪给出。
此外,如果我尝试注入一个自定义记录器,我得到错误,因为未定义的服务。
这里是我的代码:
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是givin g我错误“undefinded service”
有人可以告诉我我错在哪里吗?
THX很多。
答
试试这个:
use Monolog\Logger;
,而不是这样的:
use Symfony\Component\HttpKernel\Log\LoggerInterface;
并在此之后;
public function __construct(ContainerInterface $container, EntityManager $em, Logger $logger){
这个insetad:
public function __construct(ContainerInterface $container, EntityManager $em, LoggerInterface $logger){
你想干什么?您是否想要payment.log自定义记录器? –
阅读[documentation](http://symfony.com/doc/current/reference/dic_tags.html#dic-tags-monolog)。另外:[This](http://symfony.com/doc/current/logging.html#using-a-logger-inside-a-service)。 – ccKep
@ccKep:感谢您的提示,在解决了我的命名空间问题之后,这确实实现了。是的,我想有一个自定义记录器的付款日志,但不能使用一个单独的渠道,因为它会给我错误。 – user1827297