每个会话的 Monolog 日志文件 [英] Monolog logs file for every session

查看:28
本文介绍了每个会话的 Monolog 日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 symfony 用于每天运行的 cron run 应用程序

I'm using symfony for a cron run application that runs every day

我想使用 monolog 编写一个 info 报告,然后我可以通过电子邮件发送该报告,因此我正在寻找一种方法来按会话 ID 分隔文件.示例:

I want to use monolog to write an info report that I can then email, so I'm looking for a way to separate the files by session id. Example:

report:
 type:   stream
 path:   "%kernel.logs_dir%/%kernel.environment%-%sessionid%.log"
 level:  info

这能做到吗?

推荐答案

你必须重写AppKernel:

<?php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
    protected $sessionId;

    public function __construct($environment, $debug, $sessionId)
    {
        $this->sessionId = $sessionId;

        parent::_construct($environment, $debug);
    }


    /**
     * {@inheritdoc}
     * @see \Symfony\Component\HttpKernel\Kernel::getKernelParameters()
     */
    protected function getKernelParameters()
    {
        $parameters = parent::getKernelParameters();

        // Adding session_id parameter
        $parameters['kernel.session_id'] = $this->sessionId;

        return $parameters;
    }

 /**
  * Your function here [...]
  **
}

现在您可以在 config.yml 中使用 %kernel.session_id%

Now you can use %kernel.session_id% in your config.yml

monolog:
    handlers:
        report:
            type:  stream
            path: "%kernel.logs_dir%/%kernel.environment%-%kernel.session_id%.log"
            level:  info

在你的 app.php 中:

$sessionId = '123';
$kernel = new AppKernel('prod', false, $sessionId);

您只需将 $sessionId 作为脚本的参数传递

You just have to pass $sessionId as an argument of your script

这篇关于每个会话的 Monolog 日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆