'Php - Symfony - Logger Service - EntityManager

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');
       }
  }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source