Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.0.0] - 2023-03-31
### Removed
- Dropped support for versions below PHP 8.1 and Symfony 5.
- Root prefix processor as messages can no longer be altered as the record is now an entity instead of array, and the `message` field is read-only.
### Changed
- Changed some logic to fit the new versions of the used libraries.

## [2.1.0] - 2023-03-29
### Added
- Added Symfony ^5 support.
Expand Down
32 changes: 16 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@
}
},
"require": {
"php": "^7.2 || ^8.0",
"php": ">=8.0",
"ext-json": "*",
"graylog2/gelf-php": "^1.4.2",
"monolog/monolog": "^1.24 || ^2.0",
"sentry/sdk": "^3.1",
"sentry/sentry": "^3.1",
"sentry/sentry-symfony": "^4.0",
"symfony/config": "^3.4|^4.0|^5.0",
"symfony/dependency-injection": "^3.4|^4.0|^5.0",
"symfony/framework-bundle": "^3.4.26|^4.2.7|^5.0",
"symfony/http-kernel": "^3.4|^4.0|^5.0",
"symfony/monolog-bundle": "^3.4|^5.0"
"graylog2/gelf-php": "^2.0",
"monolog/monolog": "^3.3",
"sentry/sdk": "^3.3",
"sentry/sentry": "^3.17",
"sentry/sentry-symfony": "^4.7",
"symfony/config": "^5.0",
"symfony/dependency-injection": "^5.0",
"symfony/framework-bundle": "^5.0",
"symfony/http-kernel": "^5.0",
"symfony/monolog-bundle": "^3.4"
},
"require-dev": {
"doctrine/annotations": "^1.14",
"doctrine/doctrine-bundle": "~2.6.0",
"doctrine/orm": "^2.10",
"phpunit/phpunit": "^8.5",
"symfony/yaml": "^4.3"
"doctrine/annotations": "^2.0",
"doctrine/doctrine-bundle": "^2.9",
"doctrine/orm": "^2.14",
"phpunit/phpunit": "^10.0",
"symfony/yaml": "^5.0"
},
"config": {
"bin-dir": "bin",
Expand Down
32 changes: 14 additions & 18 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
bootstrap = "vendor/autoload.php" >

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
colors="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false">
<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
</phpunit>
11 changes: 3 additions & 8 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
if (method_exists(TreeBuilder::class, 'getRootNode')) {
$treeBuilder = new TreeBuilder('paysera_logging_extra');
$rootNode = $treeBuilder->getRootNode();
} else {
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('paysera_logging_extra');
}
$treeBuilder = new TreeBuilder('paysera_logging_extra');
$rootNode = $treeBuilder->getRootNode();

$children = $rootNode->children();
$children->scalarNode('application_name')->isRequired();
Expand Down
13 changes: 5 additions & 8 deletions src/Listener/CorrelationIdListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@
namespace Paysera\LoggingExtraBundle\Listener;

use Paysera\LoggingExtraBundle\Service\CorrelationIdProvider;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class CorrelationIdListener
{
const HEADER_NAME = 'Paysera-Correlation-Id';
public const HEADER_NAME = 'Paysera-Correlation-Id';

private $correlationIdProvider;

public function __construct(CorrelationIdProvider $correlationIdProvider)
public function __construct(private CorrelationIdProvider $correlationIdProvider)
{
$this->correlationIdProvider = $correlationIdProvider;
}

public function onKernelResponse(FilterResponseEvent $event)
public function onKernelResponse(ResponseEvent $event): void
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType()) {
return;
}

Expand Down
12 changes: 4 additions & 8 deletions src/Listener/IterationEndListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,18 @@
*/
class IterationEndListener
{
private $correlationIdProvider;
private $sentryClient;
private ?ClientInterface $sentryClient;

public function __construct(
CorrelationIdProvider $correlationIdProvider,
private CorrelationIdProvider $correlationIdProvider,
ClientInterface $sentryClient = null
) {
$this->correlationIdProvider = $correlationIdProvider;
$this->sentryClient = $sentryClient instanceof ClientInterface ? $sentryClient : null;
}

public function afterIteration()
public function afterIteration(): void
{
$this->correlationIdProvider->incrementIdentifier();
if ($this->sentryClient !== null) {
$this->sentryClient->flush();
}
$this->sentryClient?->flush();
}
}
6 changes: 0 additions & 6 deletions src/Resources/config/services/processors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@
<argument>ERROR</argument>
</service>

<service id="paysera_logging_extra.processor.remove_root_prefix"
class="Paysera\LoggingExtraBundle\Service\Processor\RemoveRootPrefixProcessor">
<tag name="monolog.processor"/>
<argument>%kernel.root_dir%/..</argument>
</service>

<service id="paysera_logging_extra.processor.sentry_context"
class="Paysera\LoggingExtraBundle\Service\Processor\SentryContextProcessor">
<tag name="monolog.processor" handler="sentry"/>
Expand Down
6 changes: 3 additions & 3 deletions src/Service/CorrelationIdProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

class CorrelationIdProvider
{
private $correlationId;
private $increment;
private string $correlationId;
private int $increment;

public function __construct(string $systemName)
{
Expand All @@ -24,7 +24,7 @@ public function getCorrelationId(): string
return sprintf('%s_%s', $this->correlationId, $this->increment);
}

public function incrementIdentifier()
public function incrementIdentifier(): void
{
$this->increment++;
}
Expand Down
20 changes: 8 additions & 12 deletions src/Service/Formatter/FormatterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,21 @@
namespace Paysera\LoggingExtraBundle\Service\Formatter;

use DateTimeInterface;
use Doctrine\Common\Persistence\Proxy as LegacyProxy;
use Doctrine\Persistence\Proxy;
use Doctrine\ORM\PersistentCollection;
use Monolog\Utils;
use Throwable;

/**
* To be used on classes extending NormalizerFormatter
*/
trait FormatterTrait
{
protected function normalize($data, $depth = 0)
protected function normalize(mixed $data, $depth = 0): mixed
{
$prenormalizedData = $this->prenormalizeData($data, $depth);
$prenormalizedData = $this->preNormalizeData($data, $depth);

return parent::normalize($prenormalizedData, $depth);
}

private function prenormalizeData($data, $depth)
private function preNormalizeData($data, $depth)
{
if ($depth > 2) {
return $this->getScalarRepresentation($data);
Expand All @@ -33,7 +29,7 @@ private function prenormalizeData($data, $depth)
return $data->isInitialized() ? iterator_to_array($data) : get_class($data);
}

if ($data instanceof Proxy || $data instanceof LegacyProxy) {
if ($data instanceof Proxy) {
return $this->normalizeProxy($data);
}

Expand All @@ -48,7 +44,7 @@ private function prenormalizeData($data, $depth)
return $data;
}

private function getScalarRepresentation($data)
private function getScalarRepresentation(mixed $data): mixed
{
if (is_scalar($data) || $data === null) {
return $data;
Expand All @@ -61,13 +57,13 @@ private function getScalarRepresentation($data)
return gettype($data);
}

private function normalizeObject($data)
private function normalizeObject(mixed $data): array
{
$result = [];
foreach ((array)$data as $key => $value) {
$parts = explode("\0", $key);
$fixedKey = end($parts);
if (substr($fixedKey, 0, 2) === '__') {
if (str_starts_with($fixedKey, '__')) {
continue;
}

Expand All @@ -77,7 +73,7 @@ private function normalizeObject($data)
return $result;
}

private function normalizeProxy(Proxy $data)
private function normalizeProxy(Proxy $data): array|string
{
if ($data->__isInitialized()) {
return $this->normalizeObject($data);
Expand Down
14 changes: 8 additions & 6 deletions src/Service/Handler/SentryExtraInformationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@

use Monolog\Handler\HandlerWrapper;
use Monolog\Handler\ProcessableHandlerTrait;
use Monolog\LogRecord;
use Sentry\State\Scope;

use function Sentry\withScope;

final class SentryExtraInformationHandler extends HandlerWrapper
{
use ProcessableHandlerTrait;

public function handle(array $record): bool
public function handle(LogRecord $record): bool
{
if (!$this->isHandling($record)) {
return false;
Expand All @@ -25,15 +27,15 @@ public function handle(array $record): bool
$record['formatted'] = $this->getFormatter()->format($record);

withScope(function (Scope $scope) use ($record, &$result): void {
if (isset($record['context']['extra']) && \is_array($record['context']['extra'])) {
foreach ($record['context']['extra'] as $key => $value) {
if (isset($record['context']) && \is_array($record['context'])) {
foreach ($record['context'] as $key => $value) {
$scope->setExtra((string) $key, $value);
}
}

if (isset($record['context']['tags']) && \is_array($record['context']['tags'])) {
foreach ($record['context']['tags'] as $key => $value) {
$scope->setTag($key, $value);
if (isset($record['extra']) && \is_array($record['extra'])) {
foreach ($record['extra'] as $key => $value) {
$scope->setTag($key, (string)$value);
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/Service/Processor/CorrelationIdProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@

namespace Paysera\LoggingExtraBundle\Service\Processor;

use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;
use Paysera\LoggingExtraBundle\Service\CorrelationIdProvider;

class CorrelationIdProcessor implements ProcessorInterface
{
private $correlationIdProvider;

public function __construct(CorrelationIdProvider $correlationIdProvider)
public function __construct(private CorrelationIdProvider $correlationIdProvider)
{
$this->correlationIdProvider = $correlationIdProvider;
}

public function __invoke(array $record)
public function __invoke(LogRecord $record): LogRecord
{
$record['extra']['correlation_id'] = $this->correlationIdProvider->getCorrelationId();

return $record;
}
}
9 changes: 3 additions & 6 deletions src/Service/Processor/GroupExceptionsProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@

namespace Paysera\LoggingExtraBundle\Service\Processor;

use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;
use Sentry\SentryBundle\SentryBundle;
use Sentry\SentrySdk;
use Sentry\State\Scope;

/**
* @php-cs-fixer-ignore Paysera/php_basic_code_style_chained_method_calls
*/
class GroupExceptionsProcessor implements ProcessorInterface
{
private $exceptionsClassesToGroup;
private array $exceptionsClassesToGroup;

public function __construct(array $exceptionsClassesToGroup)
{
$this->exceptionsClassesToGroup = array_flip($exceptionsClassesToGroup);
}

public function __invoke(array $record)
public function __invoke(LogRecord $record): LogRecord
{
if (!isset($record['context']['exception'])) {
return $record;
Expand Down
28 changes: 0 additions & 28 deletions src/Service/Processor/RemoveRootPrefixProcessor.php

This file was deleted.

Loading