3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Log\Processor
;
12 use Zend\Console\Console
;
14 class RequestId
implements ProcessorInterface
21 protected $identifier;
24 * Adds a identifier for the request to the log.
26 * This enables to filter the log for messages belonging to a specific request
28 * @param array $event event data
29 * @return array event data
31 public function process(array $event)
33 if (!isset($event['extra'])) {
34 $event['extra'] = array();
37 $event['extra']['requestId'] = $this->getIdentifier();
42 * Provide unique identifier for a request
46 protected function getIdentifier()
48 if ($this->identifier
) {
49 return $this->identifier
;
52 $requestTime = (version_compare(PHP_VERSION
, '5.4.0') >= 0)
53 ?
$_SERVER['REQUEST_TIME_FLOAT']
54 : $_SERVER['REQUEST_TIME'];
56 if (Console
::isConsole()) {
57 $this->identifier
= md5($requestTime);
58 return $this->identifier
;
61 if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
62 $this->identifier
= md5($requestTime . $_SERVER['HTTP_X_FORWARDED_FOR']);
63 return $this->identifier
;
66 $this->identifier
= md5($requestTime . $_SERVER['REMOTE_ADDR']);
67 return $this->identifier
;