【问题标题】:Problem monolog doctrine channel logging, symfony 4, with php问题独白学说频道日志记录,symfony 4,用 php
【发布时间】:2019-12-17 17:56:59
【问题描述】:

我在独白学说日志中遇到问题(在 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;
    }
}

现在在示例控制器文件中: 在 TestController.php 中

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

这不是登录到mysql数据库有什么问题吗? 提前感谢您的所有提示...

【问题讨论】:

    标签: php symfony doctrine monolog


    【解决方案1】:

    【讨论】:

    • 是的,我正在尝试链接中的第一篇文章,但在 symfony 4 中扩展 AbstractController: $container->get('monolog.logger.db') 不存在......但以前的控制器它工作....
    • 所以使用底部第二个链接中描述的配置。
    • 将您的数据库通道绑定到 LoggerInterface $dbLogger var 并使用此 var 名称注入记录器,例如 public function someAction(LoggerInterface $dbLogger){} 并注入确切的“db”通道。
    • 嗨@slmder_h。我在 symfony 3.4 上也有同样的情况。我正在将参数@logger 传递给我已经完成的自定义服务,比如 SlackHandler,并且在标签上我正在传递通道。在配置文件中,我有 [类型:服务,ID:logger.slack]。奇怪的是它没有给我任何错误。不知何故,看起来该服务从未被调用过。我正在尝试获取通道的参数,在独白处理程序下,所以我不必在参数下添加新参数:..你能帮我理解为什么会发生这种情况吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 2011-11-28
    相关资源
    最近更新 更多