Symfony 2 注入记录器服务
Symfony 2 Injecting logger service
我在 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 注入记录器给我一个错误 "undefinded service"
谁能告诉我哪里错了?
非常感谢。
试试这个:
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 中使用记录器服务时遇到一个奇怪的问题:
将记录器注入服务时,我收到类型错误,因为 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 注入记录器给我一个错误 "undefinded service"
谁能告诉我哪里错了?
非常感谢。
试试这个:
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){