-
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.
Merge pull request #3 from koderhut/add-monolog-request-id
Add monolog request identifier processor
- Loading branch information
Showing
16 changed files
with
536 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace KoderHut\OnelogBundle\DependencyInjection\Compiler; | ||
|
||
use KoderHut\OnelogBundle\Monolog\RequestIdProcessor; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* Class RequestIdentifierInjectorPass | ||
* | ||
* @author Denis-Florin Rendler <[email protected]> | ||
*/ | ||
class RequestIdentifierPass implements CompilerPassInterface | ||
{ | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasParameter('onelog.enable_request_id') || true === $container->getParameter('onelog.enable_request_id')) { | ||
return; | ||
} | ||
|
||
$container->removeDefinition(RequestIdProcessor::class); | ||
} | ||
} |
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,50 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace KoderHut\OnelogBundle\Monolog; | ||
|
||
use KoderHut\OnelogBundle\Request\IdentifierInterface; | ||
use KoderHut\OnelogBundle\Request\Identifier as RequestId; | ||
|
||
/** | ||
* Class RequestIdProcessor | ||
* | ||
* @author Denis-Florin Rendler <[email protected]> | ||
*/ | ||
class RequestIdProcessor | ||
{ | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $logCount = 1; | ||
|
||
/** | ||
* @var RequestId | ||
*/ | ||
private $requestId; | ||
|
||
/** | ||
* RequestIdProcessor constructor. | ||
* | ||
* @param IdentifierInterface $requestId | ||
*/ | ||
public function __construct(IdentifierInterface $requestId = null) | ||
{ | ||
$this->requestId = $requestId ?? RequestId::generate(); | ||
} | ||
|
||
/** | ||
* Add the request id to the log entry | ||
* | ||
* @param array $record | ||
* | ||
* @return array | ||
*/ | ||
public function __invoke(array $record): array | ||
{ | ||
$record['extra']['request_id'] = $this->requestId->identifier() . '.' . $this->logCount; | ||
$this->logCount++; | ||
|
||
return $record; | ||
} | ||
} |
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,55 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace KoderHut\OnelogBundle\Request; | ||
|
||
/** | ||
* Class Identifier | ||
* | ||
* @author Denis-Florin Rendler <[email protected]> | ||
*/ | ||
class Identifier implements IdentifierInterface | ||
{ | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $identifier; | ||
|
||
/** | ||
* Identifier constructor. | ||
* | ||
* @param string $identifier | ||
*/ | ||
public function __construct(string $identifier) | ||
{ | ||
$this->identifier = $identifier; | ||
} | ||
|
||
/** | ||
* It will generate an instance based on the current date | ||
* using the format YmdHis.u | ||
* | ||
* @param string ...$salts | ||
* | ||
* @return Identifier | ||
*/ | ||
public static function generate(string ...$salts) | ||
{ | ||
$hashData = (string) (new \DateTime())->format('Ymd.His.u'); | ||
|
||
if (!empty($salts)) { | ||
$hashData .= '.' . implode('.', $salts); | ||
} | ||
|
||
return new self($hashData); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function identifier(): string | ||
{ | ||
return $this->identifier; | ||
} | ||
|
||
} |
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,19 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace KoderHut\OnelogBundle\Request; | ||
|
||
/** | ||
* Interface IdentifierInterface | ||
* | ||
* @author Denis-Florin Rendler <[email protected]> | ||
*/ | ||
interface IdentifierInterface | ||
{ | ||
|
||
/** | ||
* Return the current identifier | ||
* | ||
* @return string | ||
*/ | ||
public function identifier(): string; | ||
} |
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
69 changes: 69 additions & 0 deletions
69
Tests/DependencyInjection/Compiler/RequestIdentifierPassTest.php
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 declare(strict_types=1); | ||
|
||
namespace KoderHut\OneLogBundle\Tests\DependencyInjection\Compiler; | ||
|
||
use KoderHut\OnelogBundle\DependencyInjection\Compiler\RequestIdentifierPass; | ||
use KoderHut\OnelogBundle\Monolog\RequestIdProcessor; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
class RequestIdentifierPassTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function testExitEarlyIfRequestIdentifierIsEnabled() | ||
{ | ||
$container = $this->prophesize(ContainerBuilder::class); | ||
$container->hasParameter('onelog.enable_request_id') | ||
->shouldBeCalled() | ||
->willReturn(false) | ||
; | ||
|
||
$instance = new RequestIdentifierPass(); | ||
|
||
$instance->process($container->reveal()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function testExitEarlyIfConfigParamIsMissing() | ||
{ | ||
$container = $this->prophesize(ContainerBuilder::class); | ||
$container->hasParameter('onelog.enable_request_id') | ||
->shouldBeCalled() | ||
->willReturn(true) | ||
; | ||
$container->getParameter('onelog.enable_request_id') | ||
->shouldBeCalled() | ||
->willReturn(true) | ||
; | ||
|
||
$instance = new RequestIdentifierPass(); | ||
|
||
$instance->process($container->reveal()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function testRemoveRequestIdServiceIfConfigIsSetToFalse() | ||
{ | ||
$container = $this->prophesize(ContainerBuilder::class); | ||
$container->hasParameter('onelog.enable_request_id') | ||
->shouldBeCalled() | ||
->willReturn(true) | ||
; | ||
$container->getParameter('onelog.enable_request_id') | ||
->shouldBeCalled() | ||
->willReturn(false) | ||
; | ||
$container->removeDefinition(RequestIdProcessor::class)->shouldBeCalled(); | ||
|
||
$instance = new RequestIdentifierPass(); | ||
|
||
$instance->process($container->reveal()); | ||
} | ||
} |
Oops, something went wrong.