Hay*_*ygo 5 php symfony monolog
我在不同的文件中为不同的频道配置了记录器,但它对我不起作用。它的工作,但它写入控制台而不是指向文件。我需要在频道搜索中将日志写入文件。这是我的代码:
#app/config/config_dev.yml
monolog:
handlers:
search:
type: stream
level: error
path: "%kernel.logs_dir%/search_log.log"
channels: [search]
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: [!event, !search]
console:
type: console
channels: [!event, !doctrine, !search]
Run Code Online (Sandbox Code Playgroud)
定义的服务:
#MyBundle/Resources/config/services.yml
services:
app.logger_search:
class: Symfony\Bridge\Monolog\Logger
arguments: ["@logger"]
tags:
- {name: monolog.logger, channel: search}
Run Code Online (Sandbox Code Playgroud)
现在使用它服务,尝试测试它:
#MyController.php
/**
* @Route("/test")
*/
public function test()
{
$this->get("app.logger_search")->error("Test");
return $this->json("test");
}
Run Code Online (Sandbox Code Playgroud)
但它写入文件的控制台。控制台我的意思是我在哪里运行我的服务器:php bin\console server:run.
创建您自己的频道。这是通过配置或通过标记您的服务并monolog.logger指定服务应登录到哪个通道来完成的(就像您所做的那样)。
两种方式都是有效的,在这两种情况下,您的记录器都将被命名:
monolog.logger.<you-channel-name>
Run Code Online (Sandbox Code Playgroud)
因此,请使用monolog.logger.search而不是您的服务 IDapp.logger_search来解决问题。
如果您没有充分的理由更改记录器行为,我建议配置不带标记服务的其他通道:
# app/config/config.yml
monolog:
channels: ['foo', 'bar']
Run Code Online (Sandbox Code Playgroud)
这样,您现在可以foo使用自动注册的记录器服务将日志消息发送到通道monolog.logger.foo。
| 归档时间: |
|
| 查看次数: |
1986 次 |
| 最近记录: |