Skip to content

Commit

Permalink
Add HighResolutionTimestampProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-daubois committed Nov 6, 2023
1 parent 083f2c5 commit 26411ee
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Latest Stable Version](http://poser.pugx.org/alexandre-daubois/monolog-processor-collection/v/stable)](https://packagist.org/packages/alexandre-daubois/monolog-processor-collection)
[![License](http://poser.pugx.org/alexandre-daubois/monolog-processor-collection/license)](https://packagist.org/packages/alexandre-daubois/monolog-processor-collection)

Monolog Processor Collectior, or **MPC** for short, is a collection of useful processors for the
Monolog Processor Collection, or **MPC** for short, is a collection of useful processors for the
[Monolog](https://github.com/Seldaek/monolog) logging library. The processors
add useful information to the log records. The package is compatible with PHP 8.1+.

Expand Down Expand Up @@ -54,6 +54,7 @@ The package provides the following processors:

- `BacktraceProcessor` adds the backtrace to the log record
- `EnvVarProcessor` adds the value of one or more environment variables to the log record
- `HighResolutionTimestampProcessor` adds the high resolution time to the log record
- `IsHttpsProcessor` adds a boolean value indicating whether the request is a secured HTTP request to the log record
- `PhpIniValueProcessor` adds the value of one or more PHP ini settings to the log record
- `ProtocolVersionProcessor` adds the HTTP protocol version to the log record
Expand Down
3 changes: 3 additions & 0 deletions src/BacktraceProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

use Monolog\LogRecord;

/**
* Add the backtrace to the log record.
*/
final class BacktraceProcessor extends AbstractThresholdProcessor
{
protected function process(LogRecord $record): LogRecord
Expand Down
3 changes: 3 additions & 0 deletions src/EnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Monolog\Level;
use Monolog\LogRecord;

/**
* Add the value of the environment variables to the log record.
*/
final class EnvVarProcessor extends AbstractThresholdProcessor
{
/**
Expand Down
25 changes: 25 additions & 0 deletions src/HighResolutionTimestampProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* (c) Alexandre Daubois <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace MonologProcessorCollection;

use Monolog\LogRecord;

/**
* Add the high resolution timestamp to the log record.
*/
final class HighResolutionTimestampProcessor extends AbstractThresholdProcessor
{
protected function process(LogRecord $record): LogRecord
{
$record['extra']['hrtime'] = \hrtime(true);

return $record;
}
}
5 changes: 4 additions & 1 deletion src/IsHttpsProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

use Monolog\LogRecord;

class IsHttpsProcessor extends AbstractThresholdProcessor
/**
* Add the high resolution timestamp to the log record.
*/
final class IsHttpsProcessor extends AbstractThresholdProcessor
{
protected function process(LogRecord $record): LogRecord
{
Expand Down
3 changes: 3 additions & 0 deletions src/PhpIniValueProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Monolog\Level;
use Monolog\LogRecord;

/**
* Add the value of some php.ini settings to the log record.
*/
final class PhpIniValueProcessor extends AbstractThresholdProcessor
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/ProtocolVersionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

use Monolog\LogRecord;

/**
* Add the protocol version to the log record.
*/
final class ProtocolVersionProcessor extends AbstractThresholdProcessor
{
protected function process(LogRecord $record): LogRecord
Expand Down
3 changes: 3 additions & 0 deletions src/ResourceUsagesProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

use Monolog\LogRecord;

/**
* Add the resource usages to the log record.
*/
final class ResourceUsagesProcessor extends AbstractThresholdProcessor
{
protected function process(LogRecord $record): LogRecord
Expand Down
3 changes: 3 additions & 0 deletions src/SapiNameProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

use Monolog\LogRecord;

/**
* Add the SAPI name to the log record.
*/
final class SapiNameProcessor extends AbstractThresholdProcessor
{
protected function process(LogRecord $record): LogRecord
Expand Down
34 changes: 34 additions & 0 deletions tests/HighResolutionTimestampProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* (c) Alexandre Daubois <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace MonologProcessorCollection\Tests;

use Monolog\Handler\TestHandler;
use Monolog\Level;
use MonologProcessorCollection\HighResolutionTimestampProcessor;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(HighResolutionTimestampProcessor::class)]
class HighResolutionTimestampProcessorTest extends AbstractProcessorTestCase
{
public function testItWorks(): void
{
$processor = new HighResolutionTimestampProcessor();

$handler = new TestHandler();
$handler->pushProcessor($processor);
$handler->handle($this->createRecord(Level::Notice));

$this->assertTrue($handler->hasNoticeRecords());
$record = $handler->getRecords()[0];

$this->assertArrayHasKey('hrtime', $record->extra);
$this->assertIsNotArray($record->extra['hrtime']);
}
}

0 comments on commit 26411ee

Please sign in to comment.