2

I want to create for each session an own logfile.
I have seen that it is possible to create an own logger (source):

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));

// add records to the log
$log->addWarning('Foo');
$log->addError('Bar');

Is it possible to connect it to a session directly using a service or implemented monolog handler?
Overwriting the path is clear for me.

Community
  • 1
  • 1
CSchulz
  • 10,882
  • 11
  • 60
  • 114

1 Answers1

0

You can create your own handler :

class LogSessionHandler extends AbstractProcessingHandler
{
  protected $session;

  public function setSession(\Symfony\Component\HttpFoundation\Session\Session $session)
  {
    $this->session = $session;
  }

  protected function write(array $record)
  {
    $fp = fopen('/path/to/log/'.$this->session->getId().'.log','a');
    if ($fp) fwrite($fp, (string) $record['formatted']);
    fclose($fp);
  }
}

In services.yml:

logger_session:
    class: ..\..\SessionDatabaseHandler
    calls:
        - [ setSession, [ @session ] ]

In config_prod.yml:

monolog:
    handlers:
        ...
        session:
            type: service
            level: error
            id: logger_session
CSchulz
  • 10,882
  • 11
  • 60
  • 114
griotteau
  • 1,772
  • 12
  • 15