1
0
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:
Dawid Nowak
2014-07-01 20:41:03 +02:00
parent 9aba3242f6
commit 6a4b73468e
2 changed files with 26 additions and 2 deletions

View File

@@ -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]);
}
}
}
} }
/** /**

View File

@@ -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
*/ */