symfony - 如何在 Symfony 命令中获取指定通道的记录器?
问题描述
我正在尝试创建一个 symfony 命令,并且我想记录命令执行期间发生的情况。所以我尝试在 monolog.yaml 中创建一个记录器通道:
monolog:
channels: ['download_site']
handlers:
download_site:
type: stream
path: "%kernel.logs_dir%/download_site_%kernel.environment%.log"
level: debug
channels: ["download_site"]
并进入频道
class DownloadSiteCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$logger = $this->getContainer()->get('monolog.logger.download_site');
}
}
但是当我执行命令时,错误抛出:
在 DownloadSiteCommand.php 第 31 行:
试图调用“App\Command\Do wnloadSiteCommand”类的名为“getContainer”的未定义方法。
解决方案
尝试这样的事情
use Psr\Log\LoggerInterface;
class DownloadSiteCommand extends Command
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->logger->info('...');
}
}
服务.yaml
App\Command\DownloadSiteCommand:
tags:
- { 'name': 'monolog.logger', 'channel': 'download_site' }
推荐阅读
- javascript - 检查一个div是否水平滚动到右端?
- python - 我怎样才能摆脱 `''` 和 `()` 字符?
- categorical-data - catboost 中每个分类值的最小样本数
- android - 将数据插入数据库时,我没有收到对 onChange 的更改
- c# - 如何防止在 RenderWindow 内绘制的图像自动缩放?
- android - 我可以向 Firebase 性能缓慢渲染事件添加额外数据吗
- ios - 即使应用程序在 iOS 中被杀死,如何调用延迟一天的方法?
- sql-server - 将 TSQL 中的字符串处理为带有时区的 DateTime
- scala - 无法在 SBT 项目中导入 MongoDB Scala 驱动程序
- java - 无法解析 javax.xml.xquery.XQException