4

我花了几个小时浏览独白文档.. 我只是无法弄清楚这个基本问题:独白中的频道是如何定义的?我得到了这个文件中有这个的 symfony2 项目config.yml

monolog:
    handlers:
        api:
            type:  stream
            path:  "%kernel.logs_dir%/%kernel.environment%_api_request_raw.log"
            channels: api
            formatter: monolog.formatter.session_api
        api_low:
            type:  stream
            path:  "%kernel.logs_dir%/%kernel.environment%_api_request_low.log"
            channels: api_low
            formatter: monolog.formatter.session_api
        car_location:
            type:  stream
            path:  "%kernel.logs_dir%/car locations/%kernel.environment%_carLocation_not-recognized.log"
            channels: not_recognized
            level: %log_debug_level%
            formatter: monolog.formatter.session_location
        active_request:
            type:  stream
            path:  "%kernel.logs_dir%/%kernel.environment%_api_active_request.log"
            channels: active_request
            level: %log_debug_level%
            formatter: monolog.formatter.session_location
        get_request:
            type:  stream
            path:  "%kernel.logs_dir%/%kernel.environment%_api_get_request.log"
            channels: get_request
            level: %log_debug_level%
            formatter: monolog.formatter.session_location

现在在服务下的同一个文件中,我得到了这些:

services:
    monolog.formatter.session_location:
        class: Monolog\Formatter\LineFormatter
        arguments:
            - "[%%datetime%%] %%level_name%%: %%message%%\n"

    monolog.formatter.session_api:
        class: Monolog\Formatter\LineFormatter
        arguments:
            - "[%%datetime%%] %%channel%%.%%level_name%%: %%message%%\n"

这基本上自定义了日志消息..很公平..

明白的部分是 api_low 是什么意思?它在哪里定义?api_low 与 api 有何不同?例如,我在这里看到处理程序名称与通道相同(即 api 处理程序有一个 api 通道).. 但我不知道该怎么做

如果我简单地放一个

 $logger = $this->get("logger");
 $logger->debug("hello world");

在我的代码中,我在控制台中得到了这个日志:

[2014-05-16 15:38:15] app.DEBUG: hello world [] []

我也在控制台中看到了这些:

[2014-05-16 15:38:08] request.INFO: Matched route "sonata_admin_dashboard" (parameters: "_controller": "St\AdminBundle\Controller\DashboardController::dashboardAction", "_route": "sonata_admin_dashboard") [] []
[2014-05-16 15:38:08] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". [] []
[2014-05-16 15:38:08] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". [] []
[2014-05-16 15:38:08] security.DEBUG: Read SecurityContext from the session [] []
[2014-05-16 15:38:08] security.DEBUG: Reloading user from user provider. [] []
[2014-05-16 15:38:08] doctrine.INFO: MongoDB query: {"find":true,"query":{"_id":{"$id":"51c8c071efe5b8dd09000003"},"type":{"$in":["user","client","partner"]}},"fields":[],"db":"smarttaxi_dev","collection":"User"} [] []
[2014-05-16 15:38:08] doctrine.INFO: MongoDB query: {"limit":true,"limitNum":1,"query":{"_id":{"$id":"51c8c071efe5b8dd09000003"},"type":{"$in":["user","client","partner"]}},"fields":[]} [] []
[2014-05-16 15:38:08] doctrine.INFO: MongoDB query: {"limit":true,"limitNum":1,"query":{"_id":{"$id":"51c8c071efe5b8dd09000003"},"type":{"$in":["user","client","partner"]}},"fields":[]} [] []
[2014-05-16 15:38:08] doctrine.INFO: MongoDB query: {"limit":true,"limitNum":1,"query":{"_id":{"$id":"51c8c071efe5b8dd09000003"},"type":{"$in":["user","client","partner"]}},"fields":[]} [] []
[2014-05-16 15:38:08] security.DEBUG: Username "aiia" was reloaded from user provider. [] []
[2014-05-16 15:38:08] event.DEBUG: Notified event "kernel.request" to listener "FOS\RestBundle\EventListener\BodyListener::onKernelRequest". [] []
[2014-05-16 15:38:08] event.DEBUG: Notified event "kernel

其中一些是显而易见的:学说匹配相关的东西,但我只是不知道我在哪里可以找到这些东西的定义或参考。

换一种方式问:api_low 是独白定义的特殊键吗?如果是这样,它的文档在哪里?我在所有代码中都找不到这个字符串的任何实例,所以我假设它是一个预定义的变量,对吧?

4

1 回答 1

5

不,api_low不是特殊键,它是开发人员定义的键,用于分隔日志(并可能以不同的方式管理它们,例如在不同的 *.log 文件中)。

有关更多详细信息,请参阅关于 Monolog如何在服务中使用自定义通道的食谱。

[编辑:示例]

我假设:

  • config.yml 如您的原始帖子中所述
  • 你有一个名为的服务MyService,它有一个功能doSomeStuff()
  • 你熟悉 Symfony 的服务理念

service.yml

myService.doSomeStuff:
        class: MyProject\MyBundle\Service\MyService
        arguments: [ "@logger"]
        tags:
            - { name: monolog.logger, channel: api_low }

MyService.php

protected $apiLowLogger;    

public function __construct($apiLowLogger)
{
    $this->apiLowLogger = $apiLowLogger;
}

public function doSomeStuff()
{
    $this->apiLowLogger->debug('This debug message will be written in %kernel.logs_dir%/%kernel.environment%_api_request_low.log');
}

要了解不同类型的日志(debug()error()info()等),请参阅源代码永久链接)。

于 2014-05-16T13:22:58.007 回答