| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Monolog package. |
| 5 |
* |
| 6 |
* (c) Jordi Boggiano <j.boggiano@seld.be> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Injects url/method and remote IP of the current web request in all records |
| 14 |
* |
| 15 |
* @author Jordi Boggiano <j.boggiano@seld.be> |
| 16 |
*/ |
| 17 |
class Monolog_Processor_WebProcessor implements Monolog_Processor_ProcessorInterface |
| 18 |
{ |
| 19 |
protected $serverData; |
| 20 |
|
| 21 |
/** |
| 22 |
* @param mixed $serverData array or object w/ ArrayAccess that provides access to the $_SERVER data |
| 23 |
*/ |
| 24 |
public function __construct($serverData = null) |
| 25 |
{ |
| 26 |
if (null === $serverData) { |
| 27 |
$this->serverData = & $_SERVER; |
| 28 |
} elseif (is_array($serverData) || $serverData instanceof ArrayAccess) { |
| 29 |
$this->serverData = $serverData; |
| 30 |
} else { |
| 31 |
throw new UnexpectedValueException('$serverData must be an array or object implementing ArrayAccess.'); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @param array $record |
| 37 |
* |
| 38 |
* @return array |
| 39 |
*/ |
| 40 |
public function callback(array $record) |
| 41 |
{ |
| 42 |
// skip processing if for some reason request data |
| 43 |
// is not present (CLI or wonky SAPIs) |
| 44 |
if (!isset($this->serverData['REQUEST_URI'])) { |
| 45 |
return $record; |
| 46 |
} |
| 47 |
|
| 48 |
$record['extra'] = array_merge( |
| 49 |
$record['extra'], |
| 50 |
array( |
| 51 |
'url' => $this->serverData['REQUEST_URI'], |
| 52 |
'ip' => isset($this->serverData['REMOTE_ADDR']) ? $this->serverData['REMOTE_ADDR'] : null, |
| 53 |
'http_method' => isset($this->serverData['REQUEST_METHOD']) ? $this->serverData['REQUEST_METHOD'] : null, |
| 54 |
'server' => isset($this->serverData['SERVER_NAME']) ? $this->serverData['SERVER_NAME'] : null, |
| 55 |
'referrer' => isset($this->serverData['HTTP_REFERER']) ? $this->serverData['HTTP_REFERER'] : null, |
| 56 |
) |
| 57 |
); |
| 58 |
|
| 59 |
if (isset($this->serverData['UNIQUE_ID'])) { |
| 60 |
$record['extra']['unique_id'] = $this->serverData['UNIQUE_ID']; |
| 61 |
} |
| 62 |
|
| 63 |
return $record; |
| 64 |
} |
| 65 |
} |
| 66 |
|