Monolog logs file for every session

一曲冷凌霜 提交于 2019-12-13 01:26:53

问题


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

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

Can this be done?


回答1:


You have to override the 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 [...]
  **
}

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

And in your app.php:

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

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



来源:https://stackoverflow.com/questions/29630818/monolog-logs-file-for-every-session

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!