0

im have problem in monolog doctrine logging (in symfony 5):

framework.yaml:

monolog:
    channels: [doctrine_channel]
    handlers:

        main:
            channels: ["!event", "!doctrine_channel"]
        doctrine:
            type: service
            channels: [doctrine_channel]
            id: app.logger.doctrine_handler

services.yaml:

  app.logger.doctrine_handler:
        class: App\Util\DoctrineHandler
        arguments:
            - "@doctrine.orm.entity_manager"

src/Utils/DoctrineHandler.php:

<?

namespace App\Util;

use App\Entity\Logs;
use Doctrine\ORM\EntityManagerInterface;
use Monolog\Handler\AbstractProcessingHandler;

class DoctrineHandler extends AbstractProcessingHandler
{
    private $initialized;
    private $entityManager;
    private $channel = 'doctrine_channel';

    public function __construct(EntityManagerInterface $entityManager)
    {
        parent::__construct();

        $this->entityManager = $entityManager;
    }

    protected function write(array $record): void
    {
        if (!$this->initialized) {
            $this->initialize();
        }

        if ($this->channel != $record['channel']) {
            return;
        }

        $log = new Logs();
        //$log->setMessage($record['message']);
        //$log->setLevel($record['level_name']);
        $log->setMessage($record['message']);
        $log->setLevel($record['level']);
        $log->setLevelName($record['level_name']);
        $log->setExtra($record['extra']);
        $log->setContext($record['context']);

        $this->entityManager->persist($log);
        $this->entityManager->flush();
    }

    private function initialize()
    {
        $this->initialized = true;
    }
}

Now in the sample controller file: in TestController.php

// in use inject LoggerInterface $logger
        $this->logger = $logger;
        $this->logger->info('test');

This is not logging to mysql database what is problem ? Thank you in advance for all the hints...

prem111
  • 63
  • 1
  • 7

1 Answers1

0

You need to to use bindings. Try this guide https://nehalist.io/logging-events-to-database-in-symfony/ And this https://symfony.com/blog/new-in-symfony-4-2-autowiring-by-type-and-name

slmder_h
  • 201
  • 1
  • 4
  • yes, im trying the first article in the link, but in symfony 4 in extends AbstractController: $container->get('monolog.logger.db') not exists... but previous Controller it works.... – prem111 Dec 17 '19 at 18:52
  • So use config as described in the bottom second link. – slmder_h Dec 17 '19 at 18:54
  • Bind you database channel to LoggerInterface $dbLogger var and inject the logger with this var name like public function someAction(LoggerInterface $dbLogger){} and exact “db” channel be injected. – slmder_h Dec 17 '19 at 18:58
  • Hi @slmder_h. I have the same situation on symfony 3.4. I'm passing the argument @ logger to a custom service that I have done, something like SlackHandler, and on the tag I'm passing the channel. On the config file I have [ type: service, id: logger.slack]. The strange thing is that it's not giving me any error. Somehow it looks like the service is never called. I'm trying to get the parameters of the channel, under the monolog handlers, so I don't have to add new ones under parameters: .. Can you help me understand why this happens? – Joan Mar 27 '20 at 19:10
  • @prem111 Could you able to solve ? I am also not able to store in the table. – user1687891 Jul 06 '22 at 10:37