-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RequestSizeProcessor and improve resettable processors
- Loading branch information
1 parent
d7a56db
commit 8b653b6
Showing
11 changed files
with
190 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/* | ||
* (c) Alexandre Daubois <alex.daubois@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace MonologProcessorCollection; | ||
|
||
use Monolog\LogRecord; | ||
use Monolog\Processor\ProcessorInterface; | ||
use Monolog\ResettableInterface; | ||
|
||
/** | ||
* Add the request size to the log record. | ||
*/ | ||
final class RequestSizeProcessor implements ProcessorInterface, ResettableInterface | ||
{ | ||
private static ?array $size = null; | ||
|
||
public function __invoke(LogRecord $record): LogRecord | ||
{ | ||
if (null === self::$size) { | ||
$headers = $this->getHeaders(); | ||
$headersSize = \array_sum( | ||
\array_map(\strlen(...), \array_keys($headers)) | ||
) + \array_sum( | ||
\array_map(\strlen(...), $headers) | ||
); | ||
|
||
$bodySize = \strlen(\file_get_contents('php://input')); | ||
self::$size = [ | ||
'headers' => $headersSize, | ||
'body' => $bodySize, | ||
'total' => $headersSize + $bodySize, | ||
]; | ||
} | ||
|
||
$record['extra']['request_size'] = self::$size; | ||
|
||
return $record; | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
private function getHeaders(): array | ||
{ | ||
if (\function_exists('apache_request_headers')) { | ||
return \apache_request_headers(); | ||
} | ||
|
||
$headers = []; | ||
foreach ($_SERVER as $k => $v) { | ||
if (\str_starts_with($k, 'HTTP_')) { | ||
$headers[\substr($k, 5)] = $v; | ||
} | ||
} | ||
|
||
return $headers; | ||
} | ||
|
||
public function reset(): void | ||
{ | ||
self::$size = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/* | ||
* (c) Alexandre Daubois <alex.daubois@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Monolog\Handler\TestHandler; | ||
use Monolog\Level; | ||
use MonologProcessorCollection\RequestSizeProcessor; | ||
use MonologProcessorCollection\Tests\AbstractProcessorTestCase; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
|
||
#[CoversClass(RequestSizeProcessor::class)] | ||
class RequestSizeProcessorTest extends AbstractProcessorTestCase | ||
{ | ||
public function testItWorks(): void | ||
{ | ||
$processor = new RequestSizeProcessor(); | ||
|
||
$handler = new TestHandler(); | ||
$handler->pushProcessor($processor); | ||
|
||
$_SERVER['HTTP_USER_AGENT'] = 'My agent'; | ||
|
||
$handler->handle($this->createRecord(Level::Notice)); | ||
$this->assertTrue($handler->hasNoticeRecords()); | ||
$record = $handler->getRecords()[0]; | ||
|
||
$this->assertArrayHasKey('request_size', $record->extra); | ||
$this->assertEquals(\strlen('USER_AGENT') + \strlen('My agent'), $record->extra['request_size']['headers']); | ||
} | ||
|
||
public function testResettable(): void | ||
{ | ||
$processor = new RequestSizeProcessor(); | ||
|
||
$handler = new TestHandler(); | ||
$handler->pushProcessor($processor); | ||
|
||
$_SERVER['HTTP_USER_AGENT'] = 'My agent'; | ||
|
||
$handler->handle($this->createRecord(Level::Notice)); | ||
$record = $handler->getRecords()[0]; | ||
|
||
$this->assertEquals(\strlen('USER_AGENT') + \strlen('My agent'), $record->extra['request_size']['headers']); | ||
|
||
$processor->reset(); | ||
|
||
$_SERVER['HTTP_USER_AGENT'] = 'My agent 2'; | ||
$handler->handle($this->createRecord(Level::Notice)); | ||
$record = $handler->getRecords()[1]; | ||
$this->assertEquals(\strlen('USER_AGENT') + \strlen('My agent 2'), $record->extra['request_size']['headers']); | ||
} | ||
} |