user3886650
user3886650

Reputation: 159

Monolog logs file for every session

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?

Upvotes: 1

Views: 370

Answers (1)

Med
Med

Reputation: 2063

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

Upvotes: 1

Related Questions