我需要将所有的用户活动登录到DB中,所以我想将Monolog存储到数据库中。
到目前为止,我已经做了以下工作:
monolog.yaml
monolog:
channels: ['db']
handlers:
db:
channels: ['db']
type: service
id: monolog.db_handlerservices.yaml
services:
...
...
monolog.db_handler:
class: App\Service\MonologDBHandler
arguments: ['@doctrine.orm.entity_manager']MonologDBHandler.php
use App\Entity\Log;
use Doctrine\ORM\EntityManagerInterface;
use Monolog\Handler\AbstractProcessingHandler;
class MonologDBHandler extends AbstractProcessingHandler
{
public function __construct(private readonly EntityManagerInterface $em)
{
parent::__construct();
}
protected function write(array $record): void
{
$logEntry = new Log();
$logEntry->setContext($record['context']);
$this->em->persist($logEntry);
$this->em->flush();
}
}在我的控制器里,
$this->logger->info('something happened');我没有看到任何插入到我的数据库。有人能帮帮我吗?
https://stackoverflow.com/questions/72627622
复制相似问题