|
0
|
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 |
namespace Monolog\Processor; |
|
|
13 |
|
|
|
14 |
/** |
|
|
15 |
* Injects url/method and remote IP of the current web request in all records |
|
|
16 |
* |
|
|
17 |
* @author Jordi Boggiano <j.boggiano@seld.be> |
|
|
18 |
*/ |
|
|
19 |
class WebProcessor |
|
|
20 |
{ |
|
|
21 |
protected $serverData; |
|
|
22 |
|
|
|
23 |
/** |
|
|
24 |
* @param mixed $serverData array or object w/ ArrayAccess that provides access to the $_SERVER data |
|
|
25 |
*/ |
|
|
26 |
public function __construct($serverData = null) |
|
|
27 |
{ |
|
|
28 |
if (null === $serverData) { |
|
|
29 |
$this->serverData =& $_SERVER; |
|
|
30 |
} elseif (is_array($serverData) || $serverData instanceof \ArrayAccess) { |
|
|
31 |
$this->serverData = $serverData; |
|
|
32 |
} else { |
|
|
33 |
throw new \UnexpectedValueException('$serverData must be an array or object implementing ArrayAccess.'); |
|
|
34 |
} |
|
|
35 |
} |
|
|
36 |
|
|
|
37 |
/** |
|
|
38 |
* @param array $record |
|
|
39 |
* @return array |
|
|
40 |
*/ |
|
|
41 |
public function __invoke(array $record) |
|
|
42 |
{ |
|
|
43 |
// skip processing if for some reason request data |
|
|
44 |
// is not present (CLI or wonky SAPIs) |
|
|
45 |
if (!isset($this->serverData['REQUEST_URI'])) { |
|
|
46 |
return $record; |
|
|
47 |
} |
|
|
48 |
|
|
|
49 |
$record['extra'] = array_merge( |
|
|
50 |
$record['extra'], |
|
|
51 |
array( |
|
|
52 |
'url' => $this->serverData['REQUEST_URI'], |
|
|
53 |
'ip' => $this->serverData['REMOTE_ADDR'], |
|
|
54 |
'http_method' => $this->serverData['REQUEST_METHOD'], |
|
|
55 |
) |
|
|
56 |
); |
|
|
57 |
|
|
|
58 |
return $record; |
|
|
59 |
} |
|
|
60 |
} |