I use symfony 2.5, and I have next service for monolog.
<?php
namespace Megogo\CoreBundle;
use Symfony\Component\HttpFoundation\Session\Session;
class SessionRequestProcessor
{
/**
* @var \Symfony\Component\HttpFoundation\Session\Session
*/
protected $session;
/**
* @var
*/
private $token;
public function __construct( Session $session)
{
$this->session = $session;
}
public function processRecord(array $record)
{
if (null === $this->token) {
try {
$this->token = $this->session->getId();
} catch (\RuntimeException $e) {
$this->token = '????????';
}
}
$record['extra']['token'] = $this->token;
return $record;
}
}
service.yml
services:
monolog.formatter.session_request:
class: Monolog\Formatter\LineFormatter
arguments:
- "[%%datetime%%] [%%extra.token%%] %%channel%%.%%level_name%%: %%message%%\n"
monolog.processor.session_request:
class: Megogo\CoreBundle\SessionRequestProcessor
arguments: ["@session"]
tags:
- { name: monolog.processor, method: processRecord }
I get example from official documentation. Adding a Session/Request Token
I have problem, that $this->session->getId()
return empty string. If I add $session->start();
all works. I can get session id. But this is weird, because in other my services all works without this workaround. And when I make app/console cache:clear
I have error Failed to start the session: already started by PHP.