-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Diego <[email protected]> Co-authored-by: Victor Del Valle <[email protected]>
- Loading branch information
0 parents
commit 332a877
Showing
28 changed files
with
963 additions
and
0 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,20 @@ | ||
# phpstorm project files | ||
.idea | ||
|
||
# netbeans project files | ||
nbproject/* | ||
|
||
# zend studio for eclipse project files | ||
.buildpath | ||
.project | ||
.settings | ||
|
||
# windows thumbnail cache | ||
Thumbs.db | ||
|
||
# Mac DS_Store Files | ||
.DS_Store | ||
|
||
/vendor/ | ||
composer.lock | ||
/.phpunit.result.cache |
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,15 @@ | ||
FROM php:8.0-cli-alpine | ||
|
||
RUN apk update && \ | ||
apk add --no-cache \ | ||
libzip-dev \ | ||
git \ | ||
openssl-dev && \ | ||
docker-php-ext-install -j$(nproc) \ | ||
zip | ||
|
||
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer | ||
|
||
ENV PATH /var/app/bin:/var/app/vendor/bin:$PATH | ||
|
||
WORKDIR /var/app |
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,24 @@ | ||
UID=$(shell id -u) | ||
GID=$(shell id -g) | ||
CONTAINER=php | ||
|
||
start: erase cache-folders build composer-install bash | ||
|
||
erase: | ||
docker-compose down -v | ||
|
||
build: | ||
docker-compose build && \ | ||
docker-compose pull | ||
|
||
cache-folders: | ||
mkdir -p ~/.composer && chown ${UID}:${GID} ~/.composer | ||
|
||
composer-install: | ||
docker-compose run --rm -u ${UID}:${GID} ${CONTAINER} composer install | ||
|
||
bash: | ||
docker-compose run --rm -u ${UID}:${GID} ${CONTAINER} sh | ||
|
||
phpunit: ## execute project unit tests | ||
docker-compose run --rm -u ${UID}:${GID} ${CONTAINER} phpunit --no-coverage |
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,63 @@ | ||
{ | ||
"name": "colvin/message-logger-php", | ||
"description": "Processors for define common structure", | ||
"authors": [ | ||
{ | ||
"name": "Aaron Bernabeu Rodríguez", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"name": "Alejandro Mascort Colomer", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"name": "Alejandro García Sánchez", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"name": "Diego García", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"name": "Miquel Mariño Espinosa", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"name": "Juan Cama Villafan", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"name": "Victor del Valle", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"license": "MIT", | ||
"type": "library", | ||
"autoload": { | ||
"psr-4": { | ||
"Colvin\\MessageLogger\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Colvin\\MessageLogger\\Tests\\": "tests/" | ||
} | ||
}, | ||
"require": { | ||
"php": "^8.0", | ||
"ext-json": "*", | ||
"symfony/dependency-injection": "^6.0", | ||
"colvin/common-domain-php": "^0.1.1" | ||
}, | ||
"require-dev": { | ||
"phpro/grumphp": "^1.5", | ||
"phpunit/phpunit": "^9.5" | ||
}, | ||
"scripts": { | ||
"post-install-cmd": [ | ||
"rm -rf .git/hooks", | ||
"mkdir -p .git/hooks", | ||
"cp -r ./config/hooks/* .git/hooks" | ||
] | ||
} | ||
} |
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,6 @@ | ||
#!/bin/sh | ||
|
||
SCRIPT=$(docker-compose run --no-deps --rm php sh -c "grumphp git:pre-commit" 2>&1) | ||
STATUS=$? | ||
echo "$SCRIPT" | ||
exit $STATUS |
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,8 @@ | ||
version: '3.8' | ||
|
||
services: | ||
php: | ||
build: . | ||
volumes: | ||
- .:/var/app | ||
- ~/.composer:/.composer |
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,7 @@ | ||
grumphp: | ||
tasks: | ||
composer: | ||
strict: true | ||
jsonlint: ~ | ||
phplint: ~ | ||
phpunit: ~ |
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,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
> | ||
|
||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Colvin\MessageLogger\DependencyInjection; | ||
|
||
use Colvin\MessageLogger\Processors\Domain\ExceptionProcessor; | ||
use Colvin\MessageLogger\Processors\Domain\MessageDataProcessor; | ||
use Colvin\MessageLogger\Processors\Domain\NormalizeContextProcessor; | ||
use Colvin\MessageLogger\Processors\Domain\OccurredOnProcessor; | ||
use Colvin\MessageLogger\Processors\Infrastructure\HostnameProcessor; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
final class MessageLoggerPass implements CompilerPassInterface | ||
{ | ||
public const PROCESSOR_OCCURRED_ON = 'colvin.message_logger.processor.occurred_on'; | ||
public const PROCESSOR_HOSTNAME = 'colvin.message_logger.processor.hostname'; | ||
public const PROCESSOR_MESSAGE_DATA = 'colvin.message_logger.processor.message_data'; | ||
public const PROCESSOR_NORMALIZE_CONTEXT = 'colvin.message_logger.processor.normalize_context'; | ||
public const PROCESSOR_EXCEPTION = 'colvin.message_logger.processor.exception'; | ||
|
||
public function process(ContainerBuilder $container): void | ||
{ | ||
$container->addDefinitions( | ||
[ | ||
self::PROCESSOR_OCCURRED_ON => new Definition( | ||
OccurredOnProcessor::class | ||
), | ||
self::PROCESSOR_HOSTNAME => new Definition( | ||
HostnameProcessor::class | ||
), | ||
self::PROCESSOR_MESSAGE_DATA => new Definition( | ||
MessageDataProcessor::class | ||
), | ||
self::PROCESSOR_NORMALIZE_CONTEXT => new Definition( | ||
NormalizeContextProcessor::class | ||
), | ||
self::PROCESSOR_EXCEPTION => new Definition( | ||
ExceptionProcessor::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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Shared\Logging; | ||
|
||
use Colvin\MessageLogger\Processors\MessageProcessor; | ||
use Colvin\CommonDomain\Domain\Message\Message; | ||
use Monolog\Processor\ProcessorInterface; | ||
|
||
final class MessageLoggerProcessor implements ProcessorInterface | ||
{ | ||
private array $processors; | ||
|
||
public function __construct(MessageProcessor ...$processors) | ||
{ | ||
$this->processors = $processors; | ||
} | ||
|
||
public function __invoke(array $record): array | ||
{ | ||
if (false === $this->isMessageRecord($record)) { | ||
return $record; | ||
} | ||
|
||
return \array_reduce($this->processors, static fn ($carry, callable $arg) => $arg($carry), $record); | ||
} | ||
|
||
private function isMessageRecord(array $record): bool | ||
{ | ||
if (false === \array_key_exists('context', $record)) { | ||
return false; | ||
} | ||
|
||
$context = $record['context']; | ||
|
||
if (false === \array_key_exists('message', $context) | ||
|| false === $context['message'] instanceof Message) { | ||
return false; | ||
} | ||
|
||
if (false === \array_key_exists('name', $context)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Colvin\MessageLogger\Processors\Domain; | ||
|
||
use Colvin\MessageLogger\Processors\MessageProcessor; | ||
use Colvin\MessageLogger\Processors\Serializer\AssociativeSerializer; | ||
|
||
final class ExceptionProcessor implements MessageProcessor | ||
{ | ||
public function __invoke(array $record): array | ||
{ | ||
if (false === \array_key_exists('exception', $record['context'])) { | ||
return $record; | ||
} | ||
|
||
$exception = $record['context']['exception']; | ||
$record['context']['exception'] = AssociativeSerializer::from($record['context']['exception']); | ||
|
||
if ($exception instanceof \JsonSerializable) { | ||
$record['context']['exception']['data'] = \json_encode($exception, \JSON_THROW_ON_ERROR); | ||
} | ||
|
||
if (true === \array_key_exists('trace', $record['context']['exception'])) { | ||
$record['context']['exception']['trace'] = \json_encode( | ||
$record['context']['exception']['trace'], | ||
\JSON_THROW_ON_ERROR | ||
); | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Colvin\MessageLogger\Processors\Domain; | ||
|
||
use Colvin\MessageLogger\Processors\MessageProcessor; | ||
use Colvin\CommonDomain\Domain\Message\AggregateMessage; | ||
use Colvin\CommonDomain\Domain\Message\Message; | ||
|
||
final class MessageDataProcessor implements MessageProcessor | ||
{ | ||
public function __invoke(array $record): array | ||
{ | ||
$message = $record['context']['message']; | ||
|
||
if (false === $message instanceof Message) { | ||
return $record; | ||
} | ||
|
||
$record = $this->messageData($record); | ||
|
||
return $this->aggregateData($record); | ||
} | ||
|
||
private function messageData(array $record): array | ||
{ | ||
$message = $this->getMessage($record); | ||
|
||
$record['extra']['messageId'] = $message->messageId()->value(); | ||
$record['extra']['name'] = $message::messageName(); | ||
$record['extra']['type'] = $message::messageType(); | ||
$record['extra']['payload'] = \json_encode($message->messagePayload(), \JSON_THROW_ON_ERROR); | ||
|
||
return $this->explodeAsyncApi($record, $message::messageName()); | ||
} | ||
|
||
private function explodeAsyncApi(array $record, string $asyncApiName): array | ||
{ | ||
$explodedName = \explode('.', $asyncApiName); | ||
|
||
$record['extra']['asyncapi']['organization'] = $explodedName[0] ?? ''; | ||
$record['extra']['asyncapi']['service'] = $explodedName[1] ?? ''; | ||
$record['extra']['asyncapi']['version'] = $explodedName[2] ?? ''; | ||
$record['extra']['asyncapi']['type'] = $explodedName[3] ?? ''; | ||
$record['extra']['asyncapi']['resource'] = $explodedName[4] ?? ''; | ||
$record['extra']['asyncapi']['name'] = $explodedName[5] ?? ''; | ||
|
||
return $record; | ||
} | ||
|
||
private function aggregateData(array $record): array | ||
{ | ||
$message = $this->getMessage($record); | ||
|
||
if (false === $message instanceof AggregateMessage) { | ||
return $record; | ||
} | ||
|
||
$record['extra']['aggregateId'] = $message->aggregateId()->value(); | ||
$record['extra']['occurredOn'] = $message->occurredOn()->format(\DateTimeInterface::ATOM); | ||
|
||
return $record; | ||
} | ||
|
||
private function getMessage(array $record): Message | ||
{ | ||
return $record['context']['message']; | ||
} | ||
} |
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 Colvin\MessageLogger\Processors\Domain; | ||
|
||
use Colvin\MessageLogger\Processors\MessageProcessor; | ||
|
||
final class NormalizeContextProcessor implements MessageProcessor | ||
{ | ||
public function __invoke(array $record): array | ||
{ | ||
if (false === \is_string($record['context']['message'])) { | ||
$record['context']['message'] = \json_encode($record['context']['message'], \JSON_THROW_ON_ERROR); | ||
} | ||
|
||
return $record; | ||
} | ||
} |
Oops, something went wrong.