问题独白学说频道日志记录,symfony 4,php
Problem monolog doctrine channel logging, symfony 4, with php
我在 monolog doctrine 日志中有问题(在 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 数据库,有什么问题吗?
预先感谢您的所有提示...
我在 monolog doctrine 日志中有问题(在 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 数据库,有什么问题吗? 预先感谢您的所有提示...