mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-05 20:57:36 +02:00
Merge pull request #387 from MacDada/WebProcessor_askedExtraFieldsOnly
WebProcessor: option to add only some extra fields
This commit is contained in:
@@ -18,12 +18,27 @@ namespace Monolog\Processor;
|
||||
*/
|
||||
class WebProcessor
|
||||
{
|
||||
/**
|
||||
* @var array|\ArrayAccess
|
||||
*/
|
||||
protected $serverData;
|
||||
|
||||
/**
|
||||
* @param mixed $serverData array or object w/ ArrayAccess that provides access to the $_SERVER data
|
||||
* @var array
|
||||
*/
|
||||
public function __construct($serverData = null)
|
||||
protected $extraFields = array(
|
||||
'url' => 'REQUEST_URI',
|
||||
'ip' => 'REMOTE_ADDR',
|
||||
'http_method' => 'REQUEST_METHOD',
|
||||
'server' => 'SERVER_NAME',
|
||||
'referrer' => 'HTTP_REFERER',
|
||||
);
|
||||
|
||||
/**
|
||||
* @param mixed $serverData Array or object w/ ArrayAccess that provides access to the $_SERVER data
|
||||
* @param array|null $extraFields Extra field names to be added (all available by default)
|
||||
*/
|
||||
public function __construct($serverData = null, array $extraFields = null)
|
||||
{
|
||||
if (null === $serverData) {
|
||||
$this->serverData =& $_SERVER;
|
||||
@@ -32,6 +47,14 @@ class WebProcessor
|
||||
} else {
|
||||
throw new \UnexpectedValueException('$serverData must be an array or object implementing ArrayAccess.');
|
||||
}
|
||||
|
||||
if (null !== $extraFields) {
|
||||
foreach (array_keys($this->extraFields) as $fieldName) {
|
||||
if (!in_array($fieldName, $extraFields)) {
|
||||
unset($this->extraFields[$fieldName]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,21 +69,25 @@ class WebProcessor
|
||||
return $record;
|
||||
}
|
||||
|
||||
$record['extra'] = array_merge(
|
||||
$record['extra'],
|
||||
array(
|
||||
'url' => $this->serverData['REQUEST_URI'],
|
||||
'ip' => isset($this->serverData['REMOTE_ADDR']) ? $this->serverData['REMOTE_ADDR'] : null,
|
||||
'http_method' => isset($this->serverData['REQUEST_METHOD']) ? $this->serverData['REQUEST_METHOD'] : null,
|
||||
'server' => isset($this->serverData['SERVER_NAME']) ? $this->serverData['SERVER_NAME'] : null,
|
||||
'referrer' => isset($this->serverData['HTTP_REFERER']) ? $this->serverData['HTTP_REFERER'] : null,
|
||||
)
|
||||
);
|
||||
|
||||
if (isset($this->serverData['UNIQUE_ID'])) {
|
||||
$record['extra']['unique_id'] = $this->serverData['UNIQUE_ID'];
|
||||
}
|
||||
$record['extra'] = $this->appendExtraFields($record['extra']);
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $extra
|
||||
* @return array
|
||||
*/
|
||||
private function appendExtraFields(array $extra)
|
||||
{
|
||||
foreach ($this->extraFields as $extraName => $serverName) {
|
||||
$extra[$extraName] = isset($this->serverData[$serverName]) ? $this->serverData[$serverName] : null;
|
||||
}
|
||||
|
||||
if (isset($this->serverData['UNIQUE_ID'])) {
|
||||
$extra['unique_id'] = $this->serverData['UNIQUE_ID'];
|
||||
}
|
||||
|
||||
return $extra;
|
||||
}
|
||||
}
|
||||
|
@@ -73,6 +73,21 @@ class WebProcessorTest extends TestCase
|
||||
$this->assertFalse(isset($record['extra']['unique_id']));
|
||||
}
|
||||
|
||||
public function testProcessorAddsOnlyRequestedExtraFields()
|
||||
{
|
||||
$server = array(
|
||||
'REQUEST_URI' => 'A',
|
||||
'REMOTE_ADDR' => 'B',
|
||||
'REQUEST_METHOD' => 'C',
|
||||
'SERVER_NAME' => 'F',
|
||||
);
|
||||
|
||||
$processor = new WebProcessor($server, array('url', 'http_method'));
|
||||
$record = $processor($this->getRecord());
|
||||
|
||||
$this->assertSame(array('url' => 'A', 'http_method' => 'C'), $record['extra']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException UnexpectedValueException
|
||||
*/
|
||||
|
Reference in New Issue
Block a user