Created
July 4, 2013 13:58
-
-
Save alex88/5928002 to your computer and use it in GitHub Desktop.
Add useful informations to logs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
monolog.processor.session_request: | |
class: Acme\CoreBundle\Service\sessionRequestProcessor | |
arguments: [ @session, @service_container, @kernel ] | |
tags: | |
- { name: monolog.processor, method: processRecord } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\CoreBundle\Service; | |
use Symfony\Component\HttpFoundation\Session\Session; | |
use Symfony\Component\Security\Core\SecurityContext; | |
use Symfony\Component\HttpKernel\Kernel; | |
use Acme\UserBundle\Entity\User; | |
/** | |
* Class for adding the session and a random token to the log data | |
* | |
* (c) Alessandro Tagliapietra <[email protected]> | |
*/ | |
class sessionRequestProcessor | |
{ | |
private $session; | |
private $token; | |
private $sessionId; | |
private $securityContext; | |
private $kernel; | |
/** | |
* Creates the service | |
* | |
* @param Session $session Sets the internal session object | |
* @param Container $container The service container | |
* @param kernel $kernel The symfony kernel to get the environment from | |
*/ | |
public function __construct(Session $session, $container, Kernel $kernel) | |
{ | |
$this->session = $session; | |
$this->container = $container; | |
$this->kernel = $kernel; | |
} | |
/** | |
* Adds the session id to the log record to filter by session | |
* Gets the session id from the session object but sometimes it's not yet initalized | |
* So it gets directly from the PHPSESSID cookie | |
* | |
* @param array $record the record array of informations | |
* | |
* @return array $record the record array plus the session informations | |
*/ | |
public function processRecord(array $record) | |
{ | |
if (null === $this->token) { | |
try { | |
if (!$this->session->getId()) { | |
if (isset($_COOKIE['PHPSESSID'])) { | |
$this->token = substr($_COOKIE['PHPSESSID'], 0, 8); | |
} else { | |
$this->token = 'null'; | |
} | |
} else { | |
$this->token = substr($this->session->getId(), 0, 8); | |
} | |
} catch (\RuntimeException $e) { | |
$this->token = '????????'; | |
} | |
$this->token .= '-' . substr(uniqid(), -8); | |
} | |
if (null === $this->sessionId) { | |
try { | |
if (!$this->session->getId()) { | |
if (isset($_COOKIE['PHPSESSID'])) { | |
$this->sessionId = $_COOKIE['PHPSESSID']; | |
} else { | |
$this->sessionId = 'null'; | |
} | |
} else { | |
$this->sessionId = $this->session->getId(); | |
} | |
} catch (\RuntimeException $e) { | |
$this->sessionId = '????????'; | |
} | |
} | |
$record['extra']['session'] = $this->sessionId; | |
$record['extra']['token'] = $this->token; | |
if (!is_null($this->container->get('security.context')->getToken()) && !is_null($this->container->get('security.context')->getToken()->getUser())) { | |
if ($this->container->get('security.context')->getToken()->getUser() instanceof User) { | |
$record['extra']['user'] = $this->container->get('security.context')->getToken()->getUser()->getUsername(); | |
} else { | |
$record['extra']['user'] = $this->container->get('security.context')->getToken()->getUser(); | |
} | |
} | |
$record['extra']['environment'] = $this->kernel->getEnvironment(); | |
return $record; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment