Php - Symfony - 记录器服务 - EntityManager
我向您寻求帮助,因为我在尝试创建用于跟踪的日志服务时遇到错误。 我按照本教程执行此任务: https://nehalist.io/logging-events-to-database-in -symfony/
创建日志实体时没有出现问题,并且 dbHandler 服务似乎正在运行。 当我在 services.yaml (arguments): ["@doctrine.orm.entity_manager"] 中引入参数并在 MenuController 构造函数参数中使用它时,这就是问题发生的地方。
以下是错误消息:“需要指定元数据驱动程序并将其传递给 Doctrine\ORM\Configuration::setMetadataDriverImpl()”
感谢您的帮助。 代码错误
services.yaml
services:
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event
subscribers, etc.
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
- '../src/Tests/'
App\Controller\:
resource: '../src/Controller/'
tags: ['controller.service_arguments']
arguments: ['@monolog.logger.db']
dbHandler:
class: App\Service\DbHandler
arguments: ["@doctrine.orm.entity_manager"]
monolog.yaml
monolog:
channels: ['db']
handlers:
dbHandler:
type: service
id: dbHandler
DbHandler.php
<?php
namespace App\Service;
use App\Entity\Log;
use Monolog\Handler\AbstractProcessingHandler;
use Doctrine\ORM\EntityManagerInterface;
class DbHandler extends AbstractProcessingHandler {
private $em;
public function __construct(EntityManagerInterface $em) {
parent::__construct();
$this->em = $em;
}
protected function write($record):void {
$log = new Log();
$log->setMessage($record['message']);
$log->setLevel($record['level']);
$log->setLevelName($record['level_name']);
$log->setContext($record['context']);
$this->em->persist($log);
$this->em->flush();
}
}
MenuController.php
namespace App\Controller;
use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MenuController extends AbstractController
{
private $logger;
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
/**
* @Route("/menu", name="menu")
* @IsGranted("ROLE_USER")
*/
public function menu(LoggerInterface $logger )
{
$logger->info('Test');
}
}
I'm asking you for help because I got an error while trying to create a log service for tracing.
I followed this tutorial to perform this task:
https://nehalist.io/logging-events-to-database-in-symfony/
No problem occurred when creating the Log entity and the dbHandler service seems to run.
When I introduce the argument in services.yaml (arguments): ["@doctrine.orm.entity_manager"] and I use it in the MenuController constructor parameters, that's where the problem occurs.
Here is the error message: "It's a requirement to specify a Metadata Driver and pass it to Doctrine\ORM\Configuration::setMetadataDriverImpl()"
Thanks for your help.
Code error
services.yaml
services:
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event
subscribers, etc.
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
- '../src/Tests/'
App\Controller\:
resource: '../src/Controller/'
tags: ['controller.service_arguments']
arguments: ['@monolog.logger.db']
dbHandler:
class: App\Service\DbHandler
arguments: ["@doctrine.orm.entity_manager"]
monolog.yaml
monolog:
channels: ['db']
handlers:
dbHandler:
type: service
id: dbHandler
DbHandler.php
<?php
namespace App\Service;
use App\Entity\Log;
use Monolog\Handler\AbstractProcessingHandler;
use Doctrine\ORM\EntityManagerInterface;
class DbHandler extends AbstractProcessingHandler {
private $em;
public function __construct(EntityManagerInterface $em) {
parent::__construct();
$this->em = $em;
}
protected function write($record):void {
$log = new Log();
$log->setMessage($record['message']);
$log->setLevel($record['level']);
$log->setLevelName($record['level_name']);
$log->setContext($record['context']);
$this->em->persist($log);
$this->em->flush();
}
}
MenuController.php
namespace App\Controller;
use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MenuController extends AbstractController
{
private $logger;
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
/**
* @Route("/menu", name="menu")
* @IsGranted("ROLE_USER")
*/
public function menu(LoggerInterface $logger )
{
$logger->info('Test');
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论