Php - Symfony - 记录器服务 - EntityManager

发布于 2025-01-11 14:16:01 字数 2831 浏览 0 评论 0原文

我向您寻求帮助,因为我在尝试创建用于跟踪的日志服务时遇到错误。 我按照本教程执行此任务: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文