-
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.
[MetricsPower] Added ContextExtractor for logger
[MetricsPower] Added ContextExtractor for logger
- Loading branch information
1 parent
1bfc556
commit e0948b2
Showing
32 changed files
with
874 additions
and
79 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,37 @@ | ||
<?php | ||
|
||
/** @noinspection PhpDocSignatureInspection */ | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
* | ||
* Copyright (c) 2024 Mykhailo Shtanko fractalzombie@gmail.com | ||
* | ||
* For the full copyright and license information, please view the LICENSE.MD | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FRZB\Component\MetricsPower\Logger\ContextExtractor; | ||
|
||
use FRZB\Component\MetricsPower\Attribute\OptionsInterface; | ||
use FRZB\Component\MetricsPower\Logger\Data\Context; | ||
|
||
/** | ||
* @template TTarget of mixed | ||
* @template TOptions of OptionsInterface | ||
*/ | ||
interface ContextExtractorInterface | ||
{ | ||
/** | ||
* @param TTarget $target | ||
* @param TOptions $options | ||
* @param ?\Throwable $exception | ||
*/ | ||
public function extract(mixed $target, ?OptionsInterface $options = null, ?\Throwable $exception = null): Context; | ||
|
||
public static function getType(): 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
* | ||
* Copyright (c) 2024 Mykhailo Shtanko fractalzombie@gmail.com | ||
* | ||
* For the full copyright and license information, please view the LICENSE.MD | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FRZB\Component\MetricsPower\Logger\ContextExtractor; | ||
|
||
use FRZB\Component\DependencyInjection\Attribute\AsService; | ||
use FRZB\Component\DependencyInjection\Attribute\AsTagged; | ||
use FRZB\Component\MetricsPower\Attribute\OptionsInterface; | ||
use FRZB\Component\MetricsPower\Helper\ClassHelper; | ||
use FRZB\Component\MetricsPower\Logger\Data\Context; | ||
use Symfony\Component\Serializer\Encoder\JsonEncoder; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
/** | ||
* @implements ContextExtractorInterface<object, OptionsInterface> | ||
*/ | ||
#[AsService, AsTagged(ContextExtractorInterface::class)] | ||
class DefaultContextExtractor implements ContextExtractorInterface | ||
{ | ||
public const DEFAULT_TYPE = 'Default'; | ||
private const MESSAGE_INFO = '[MetricsPower] [INFO] [MESSAGE: Operation succeed] [OPTIONS_CLASS: {options_class}] [TARGET_CLASS: {target_class}]'; | ||
private const MESSAGE_ERROR = '[MetricsPower] [ERROR] [MESSAGE: Operation failed] [OPTIONS_CLASS: {options_class}] [MESSAGE_CLASS: {message_class}] [EXCEPTION_CLASS: {exception_class}] [EXCEPTION_MESSAGE: {exception_message}] [OPTIONS_VALUES: {option_values}]'; | ||
|
||
public function __construct( | ||
private readonly SerializerInterface $serializer, | ||
) {} | ||
|
||
public function extract(mixed $target, ?OptionsInterface $options = null, ?\Throwable $exception = null): Context | ||
{ | ||
$message = $exception ? self::MESSAGE_ERROR : self::MESSAGE_INFO; | ||
$context = ['target_class' => ClassHelper::getShortName($target)]; | ||
|
||
if ($options?->isSerializable()) { | ||
$context += ['target_values' => $this->serializer->serialize($target, JsonEncoder::FORMAT)]; | ||
} | ||
|
||
if ($options) { | ||
$context += [ | ||
'options_class' => ClassHelper::getShortName($options), | ||
'option_values' => ClassHelper::getProperties($options), | ||
]; | ||
} | ||
|
||
if ($exception) { | ||
$context += [ | ||
'exception_class' => ClassHelper::getShortName($exception), | ||
'exception_message' => $exception->getMessage(), | ||
'exception_trace' => $exception->getTrace(), | ||
]; | ||
} | ||
|
||
return new Context($message, $context); | ||
} | ||
|
||
public static function getType(): string | ||
{ | ||
return self::DEFAULT_TYPE; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
Logger/ContextExtractor/ExceptionEventContextExtractor.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,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
* | ||
* Copyright (c) 2024 Mykhailo Shtanko fractalzombie@gmail.com | ||
* | ||
* For the full copyright and license information, please view the LICENSE.MD | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FRZB\Component\MetricsPower\Logger\ContextExtractor; | ||
|
||
use FRZB\Component\DependencyInjection\Attribute\AsService; | ||
use FRZB\Component\DependencyInjection\Attribute\AsTagged; | ||
use FRZB\Component\MetricsPower\Attribute\OptionsInterface; | ||
use FRZB\Component\MetricsPower\Helper\ClassHelper; | ||
use FRZB\Component\MetricsPower\Logger\Data\Context; | ||
use Symfony\Component\HttpKernel\Event\ExceptionEvent; | ||
|
||
/** | ||
* @implements ContextExtractorInterface<ExceptionEvent, OptionsInterface> | ||
*/ | ||
#[AsService, AsTagged(ContextExtractorInterface::class)] | ||
class ExceptionEventContextExtractor implements ContextExtractorInterface | ||
{ | ||
private const MESSAGE = '[MetricsPower] [ERROR] [MESSAGE: Operation failed] [TARGET_CLASS: {target_class}] [EXCEPTION_CLASS: {exception_class}] [EXCEPTION_MESSAGE: {exception_message}]'; | ||
|
||
public function extract(mixed $target, ?OptionsInterface $options = null, ?\Throwable $exception = null): Context | ||
{ | ||
$context = [ | ||
'target_class' => ClassHelper::getShortName($target), | ||
]; | ||
|
||
if ($exception) { | ||
$context += [ | ||
'exception_class' => ClassHelper::getShortName($target->getThrowable()), | ||
'exception_message' => $target->getThrowable()->getMessage(), | ||
'exception_trace' => $target->getThrowable()->getTrace(), | ||
]; | ||
} | ||
|
||
return new Context(self::MESSAGE, $context); | ||
} | ||
|
||
public static function getType(): string | ||
{ | ||
return ExceptionEvent::class; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
Logger/ContextExtractor/SendMessageToTransportsEventContextExtractor.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,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
* | ||
* Copyright (c) 2024 Mykhailo Shtanko fractalzombie@gmail.com | ||
* | ||
* For the full copyright and license information, please view the LICENSE.MD | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FRZB\Component\MetricsPower\Logger\ContextExtractor; | ||
|
||
use FRZB\Component\DependencyInjection\Attribute\AsService; | ||
use FRZB\Component\DependencyInjection\Attribute\AsTagged; | ||
use FRZB\Component\MetricsPower\Attribute\OptionsInterface; | ||
use FRZB\Component\MetricsPower\Helper\ClassHelper; | ||
use FRZB\Component\MetricsPower\Logger\Data\Context; | ||
use Symfony\Component\Messenger\Event\SendMessageToTransportsEvent; | ||
use Symfony\Component\Serializer\Encoder\JsonEncoder; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
/** | ||
* @implements ContextExtractorInterface<SendMessageToTransportsEvent, OptionsInterface> | ||
*/ | ||
#[AsService, AsTagged(ContextExtractorInterface::class)] | ||
class SendMessageToTransportsEventContextExtractor implements ContextExtractorInterface | ||
{ | ||
private const MESSAGE = '[MetricsPower] [INFO] [MESSAGE: Sent to transport] [OPTIONS_CLASS: {options_class}] [TARGET_CLASS: {target_class}] [MESSAGE_CLASS: {message_class}]'; | ||
|
||
public function __construct( | ||
private readonly SerializerInterface $serializer, | ||
) {} | ||
|
||
public function extract(mixed $target, ?OptionsInterface $options = null, ?\Throwable $exception = null): Context | ||
{ | ||
$context = [ | ||
'target_class' => ClassHelper::getShortName($target), | ||
'message_class' => ClassHelper::getShortName($target->getEnvelope()->getMessage()), | ||
]; | ||
|
||
if ($options?->isSerializable()) { | ||
$context += ['message_values' => $this->serializer->serialize($target->getEnvelope()->getMessage(), JsonEncoder::FORMAT)]; | ||
} | ||
|
||
if ($options) { | ||
$context += [ | ||
'options_class' => ClassHelper::getShortName($options), | ||
]; | ||
} | ||
|
||
return new Context(self::MESSAGE, $context); | ||
} | ||
|
||
public static function getType(): string | ||
{ | ||
return SendMessageToTransportsEvent::class; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
Logger/ContextExtractor/WorkerMessageFailedEventContextExtractor.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,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
* | ||
* Copyright (c) 2024 Mykhailo Shtanko fractalzombie@gmail.com | ||
* | ||
* For the full copyright and license information, please view the LICENSE.MD | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FRZB\Component\MetricsPower\Logger\ContextExtractor; | ||
|
||
use FRZB\Component\DependencyInjection\Attribute\AsService; | ||
use FRZB\Component\DependencyInjection\Attribute\AsTagged; | ||
use FRZB\Component\MetricsPower\Attribute\OptionsInterface; | ||
use FRZB\Component\MetricsPower\Helper\ClassHelper; | ||
use FRZB\Component\MetricsPower\Logger\Data\Context; | ||
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent; | ||
use Symfony\Component\Serializer\Encoder\JsonEncoder; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
/** | ||
* @implements ContextExtractorInterface<WorkerMessageFailedEvent, OptionsInterface> | ||
*/ | ||
#[AsService, AsTagged(ContextExtractorInterface::class)] | ||
final class WorkerMessageFailedEventContextExtractor implements ContextExtractorInterface | ||
{ | ||
private const MESSAGE = '[MetricsPower] [ERROR] [MESSAGE: Handle failed] [TARGET_CLASS: {target_class}] [OPTIONS_CLASS: {options_class}] [MESSAGE_CLASS: {message_class}] [EXCEPTION_CLASS: {exception_class}] [EXCEPTION_MESSAGE: {exception_message}] [OPTIONS_VALUES: {option_values}]'; | ||
|
||
public function __construct( | ||
private readonly SerializerInterface $serializer, | ||
) {} | ||
|
||
public function extract(mixed $target, ?OptionsInterface $options = null, ?\Throwable $exception = null): Context | ||
{ | ||
$context = [ | ||
'target_class' => ClassHelper::getShortName($target), | ||
]; | ||
|
||
if ($options?->isSerializable()) { | ||
$context += ['target_values' => $this->serializer->serialize($target->getEnvelope()->getMessage(), JsonEncoder::FORMAT)]; | ||
} | ||
|
||
if ($options) { | ||
$context += [ | ||
'options_class' => ClassHelper::getShortName($options), | ||
'option_values' => ClassHelper::getProperties($options), | ||
]; | ||
} | ||
|
||
if ($exception) { | ||
$context += [ | ||
'exception_class' => ClassHelper::getShortName($exception), | ||
'exception_message' => $exception->getMessage(), | ||
'exception_trace' => $exception->getTrace(), | ||
]; | ||
} | ||
|
||
return new Context(self::MESSAGE, $context); | ||
} | ||
|
||
public static function getType(): string | ||
{ | ||
return WorkerMessageFailedEvent::class; | ||
} | ||
} |
Oops, something went wrong.