我在配置Monolog以处理“嵌套记录器”方面遇到了一些问题。
我想做什么:
将服务记录到专用文件(每个服务一个) AND 从所有服务到一个文件。每个记录器也应由monolog.handlers.console
处理。
我为什么要这样做
每个服务都有逻辑,但可以使用DI的其他服务。我想知道一个服务日志到底是什么,所以我想为每个服务提供专用的记录器(带有自定义通道和自定义日志文件)。 但当服务依赖于其他服务时,我想在一个文件中按时间顺序读取日志。
我有什么
app/config.yml
:
monolog:
handlers:
my_handler:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.my.log
level: info
handler: my_bundle_handler
src/My/Bundle/Resources/config/config.yml
services:
# LOGGERS
my_logger:
class: Symfony\Bridge\Monolog\Logger
arguments: [my_logger]
calls:
- [pushHandler, [@monolog.handler.console]]
- [pushHandler, [@my_bundle_handler]]
tags:
- { name: monolog.logger, channel: my_channel}
# HANDLERS
my_bundle_handler:
abstract: true # Without it it will throw exception
type: group
members: [my_service_handler]
channels: ["my_channel"]
tags:
- { name: log_handler }
my_service_handler:
class: Monolog\Handler\StreamHandler
arguments: [%kernel.logs_dir%/%kernel.environment%.my_service.log, 100]
channels: ["my_channel"]
tags:
- { name: log_handler }
它没有按预期工作。它会记录到my_service.log
,但不会记录到my.log
。
是否有可能实现我的目标?
答案 0 :(得分:6)
monolog中的频道与你想要的完全一样。样本monolog配置
app/config.yml
monolog:
channels: ['deletion']
handlers:
main:
type: fingers_crossed
action_level: error
handler: grouped_main
formatter: "monolog.formatter.request"
buffer_size: 30
# if you will set stop_buffering: true - you will get ALL events after first error. It could produce huge logs for console
stop_buffering: false
# this is for getsentry.com error catching
sentry:
type: raven
dsn: '%sentry_url%'
level: notice
# Groups
grouped_main:
type: group
members: [sentry, streamed_main, streamed_main_brief]
# Streams
streamed_main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
streamed_main_brief:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%_brief.log"
formatter: monolog.brief_formatter
console:
type: console
formatter: monolog.console_formatter
deletion:
# log deletion related messages
level: debug
type: stream
path: '%kernel.logs_dir%/deletion.log'
channels: ['deletion']
formatter: monolog.brief_formatter
services:
my_service:
class: Monolog\Processor\IntrospectionProcessor
tags:
- { name: monolog.processor }
monolog.console_formatter:
class: Symfony\Bridge\Monolog\Formatter\ConsoleFormatter
arguments:
- "<fg=black;bg=green>[%%datetime%%]</fg=black;bg=green> %%start_tag%%%%message%%%%end_tag%%\n"
monolog.brief_formatter:
class: Monolog\Formatter\LineFormatter
arguments:
- "[%%datetime%%] %%message%%\n"
# default format is
# - "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
如果您要添加
等代码$this->getContainer()->get("logger")->info("Sample info");
$this->getContainer()->get("monolog.logger.deletion")->info("Deletion channel info");
$this->getContainer()->get("monolog.logger.deletion")->error("Deletion channel error");
$this->getContainer()->get("monolog.logger.deletion")->info("Deletion channel info #2");
您将获得包含此类内容的3个日志文件
频道日志文件
deletion.log
[2016-11-11 12:43:18] Deletion channel info
[2016-11-11 12:43:18] Deletion channel error
[2016-11-11 12:43:19] Deletion channel info #2
默认环境日志文件
dev.log
[2016-11-11 12:43:18] event.DEBUG: Notified event "console.command" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure". [] {"file":"...."}
[2016-11-11 12:43:18] event.DEBUG: Notified event "console.command" to listener "Symfony\Bridge\Monolog\Handler\ConsoleHandler::onCommand". [] {"file":"...."}
[2016-11-11 12:43:18] app.INFO: Sample info [] {"file":"..."}
[2016-11-11 12:43:18] deletion.INFO: Deletion channel info [] {"file":"...."}
[2016-11-11 12:43:18] deletion.ERROR: Deletion channel error [] {"...."}
简要env.log
[2016-11-11 12:43:18] Notified event "console.command" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure".
[2016-11-11 12:43:18] Notified event "console.command" to listener "Symfony\Bridge\Monolog\Handler\ConsoleHandler::onCommand".
[2016-11-11 12:43:18] Sample info
[2016-11-11 12:43:18] Deletion channel info
[2016-11-11 12:43:18] Deletion channel error
还要注意,因为stop_buffering:错误后的错误通知不会出现在dev.log,dev_brief.log中,但会出现在deletion.log
你应该尝试哨兵 - 它的伟大产品和他的主人是很酷的家伙:)。