gpt4 book ai didi

php - symfony monolog 中的 channel 是如何定义的?

转载 作者:行者123 更新时间:2023-12-04 12:19:05 25 4
gpt4 key购买 nike

我花了几个小时浏览了 monolog 文档......我只是想不通这个基本问题:如何在 monolog 中定义 channel ?我在 config.yml 中得到了这个 symfony2 项目。文件:

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 有何不同?例如,我在这里看到处理程序名称与 channel 相同(即 api 处理程序有一个 api channel )。但我不知道该怎么做

如果我只是把一个
 $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 是 monolog 定义的特殊键吗?如果是这样,它的文档在哪里?我在所有代码中都找不到这个字符串的任何实例,所以我假设它是一个预定义的变量,对吗?

最佳答案

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

Cookbook about MonologHow to use a custom channel in a service更多细节。

[编辑:示例]

我假设:

  • 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() 等),
    source ( permalink )。

    关于php - symfony monolog 中的 channel 是如何定义的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23695491/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com