mirror of
https://github.com/Seldaek/monolog.git
synced 2025-07-31 18:30:15 +02:00
WebProcessor: option to add only requested extra fields, instead of always adding all available
This commit is contained in:
@@ -35,9 +35,10 @@ class WebProcessor
|
|||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $serverData array or object w/ ArrayAccess that provides access to the $_SERVER data
|
* @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)
|
public function __construct($serverData = null, array $extraFields = null)
|
||||||
{
|
{
|
||||||
if (null === $serverData) {
|
if (null === $serverData) {
|
||||||
$this->serverData =& $_SERVER;
|
$this->serverData =& $_SERVER;
|
||||||
@@ -46,6 +47,14 @@ class WebProcessor
|
|||||||
} else {
|
} else {
|
||||||
throw new \UnexpectedValueException('$serverData must be an array or object implementing ArrayAccess.');
|
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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -73,6 +73,21 @@ class WebProcessorTest extends TestCase
|
|||||||
$this->assertFalse(isset($record['extra']['unique_id']));
|
$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
|
* @expectedException UnexpectedValueException
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user