Skip to content
Merged
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
3 changes: 2 additions & 1 deletion Monolog/Formatter/TokenCollectionFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ETSGlobal\LogBundle\Tracing\Token;
use ETSGlobal\LogBundle\Tracing\TokenCollection;
use Monolog\Formatter\LineFormatter;
use Monolog\LogRecord;

/** @internal */
final class TokenCollectionFormatter extends LineFormatter
Expand All @@ -21,7 +22,7 @@ public function __construct(
parent::__construct($originalFormat, $dateFormat, $allowInlineLineBreaks, $ignoreEmptyContextAndExtra);
}

public function format(array $record): string
public function format(LogRecord $record): string
{
$this->format = str_replace(
'%token_collection%',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace ETSGlobal\LogBundle\Monolog\Handler\ExclusionStrategy;

use Monolog\LogRecord;

/** Decides whether a record has to be excluded.*/
interface ExclusionStrategyInterface
{
public function excludeRecord(array $record): bool;
public function excludeRecord(LogRecord $record): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace ETSGlobal\LogBundle\Monolog\Handler\ExclusionStrategy;

use Monolog\LogRecord;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

/** @internal */
Expand All @@ -13,13 +14,14 @@ public function __construct(private array $excludedStatusCodes)
{
}

public function excludeRecord(array $record): bool
public function excludeRecord(LogRecord $record): bool
{
if (!isset($record['context']['exception'])) {
if (!is_array($record['context']) || !isset($record['context']['exception'])) {
return false;
}

$exception = $record['context']['exception'];

if (!$exception instanceof HttpExceptionInterface) {
return false;
}
Expand Down
11 changes: 7 additions & 4 deletions Monolog/Handler/IgnoreDeprecationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
namespace ETSGlobal\LogBundle\Monolog\Handler;

use Monolog\Handler\AbstractHandler;
use Monolog\Logger;
use Monolog\Level;
use Monolog\LogRecord;

class IgnoreDeprecationHandler extends AbstractHandler
{
public function __construct(int|string $level = Logger::DEBUG)
public function __construct(int|string|Level $level = Level::Debug)
{
parent::__construct($level, false);
}

public function handle(array $record): bool
public function handle(LogRecord $record): bool
{
return $record['channel'] === 'php' && str_contains($record['message'], 'User Deprecated');
return $record['channel'] === 'php' &&
is_string($record['message']) &&
str_contains($record['message'], 'User Deprecated');
}
}
16 changes: 6 additions & 10 deletions Monolog/Handler/SlackHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
use ETSGlobal\LogBundle\Monolog\Handler\ContentDataModifier\ContentDataModifierInterface;
use ETSGlobal\LogBundle\Monolog\Handler\ExclusionStrategy\ExclusionStrategyInterface;
use Monolog\Handler\SlackHandler as BaseSlackHandler;
use Monolog\Logger;
use Monolog\Level;
use Monolog\LogRecord;

/**
* @internal
*
* @phpstan-import-type Level from Logger
*/
/** @internal */
final class SlackHandler extends BaseSlackHandler
{
private array $exclusionStrategies = [];
Expand All @@ -22,7 +19,6 @@ final class SlackHandler extends BaseSlackHandler

/**
* {@inheritdoc}
* @phpstan-param Level $level
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -31,7 +27,7 @@ public function __construct(
?string $username = null,
bool $useAttachment = true,
?string $iconEmoji = null,
int $level = Logger::CRITICAL,
int|string|Level $level = Level::Critical,
bool $bubble = true,
bool $useShortAttachment = false,
bool $includeContextAndExtra = false,
Expand Down Expand Up @@ -61,7 +57,7 @@ public function addContentDataModifier(ContentDataModifierInterface $contentData
$this->contentDataModifiers[] = $contentDataModifier;
}

public function isHandling(array $record): bool
public function isHandling(LogRecord $record): bool
{
foreach ($this->exclusionStrategies as $exclusionStrategy) {
if (true === $exclusionStrategy->excludeRecord($record)) {
Expand All @@ -73,7 +69,7 @@ public function isHandling(array $record): bool
}

/** phpcs:disable */
protected function prepareContentData(array $record): array
protected function prepareContentData(LogRecord $record): array
{
$dataArray = parent::prepareContentData($record);

Expand Down
42 changes: 22 additions & 20 deletions Monolog/Processor/ChangeExceptionsLogLevelProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace ETSGlobal\LogBundle\Monolog\Processor;

use Monolog\Logger;
use Monolog\Level;
use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

Expand All @@ -15,9 +16,6 @@
* HttpNotFoundException we want to lower the log level to something like INFO
* because there is nothing wrong with our application.
*
* @phpstan-import-type LevelName from Logger
* @phpstan-import-type Record from Logger
* @phpstan-import-type Level from Logger
* @internal
*/
final class ChangeExceptionsLogLevelProcessor implements ProcessorInterface
Expand All @@ -26,7 +24,7 @@ public function __construct(private array $customExceptionsConfig, private array
{
}

private function determineLogLevel(\Throwable $throwable, int $currentLevel): int
private function determineLogLevel(\Throwable $throwable, int $currentLevel): Level
{
if ($throwable instanceof HttpExceptionInterface) {
return $this->determineLogLevelHttpException($throwable, $currentLevel);
Expand All @@ -36,34 +34,38 @@ private function determineLogLevel(\Throwable $throwable, int $currentLevel): in

foreach ($exceptions as $exception) {
if ($throwable instanceof $exception) {
return $this->customExceptionsConfig[$exception];
return Level::from($this->customExceptionsConfig[$exception]);
}
}

return $currentLevel;
return Level::from($currentLevel);
}

private function determineLogLevelHttpException(HttpExceptionInterface $httpException, int $currentLevel): int
{
private function determineLogLevelHttpException(
HttpExceptionInterface $httpException,
int $currentLevel,
): Level {
$exceptions = array_keys($this->httpExceptionsConfig);

foreach ($exceptions as $exception) {
if ($httpException instanceof $exception) {
return $this->httpExceptionsConfig[$exception];
return Level::from($this->httpExceptionsConfig[$exception]);
}
}

return $currentLevel;
return Level::from($currentLevel);
}

/** @phpstan-return Record */
public function __invoke(array $record): array
public function __invoke(LogRecord $record): LogRecord
{
if ($record['level'] < 400) {
/** @var int $level */
$level = $record['level'];

if ($level < 400) {
return $record;
}

if (!isset($record['context']['exception'])) {
if (!is_array($record['context']) || !isset($record['context']['exception'])) {
return $record;
}

Expand All @@ -75,12 +77,12 @@ public function __invoke(array $record): array
}

// Change the log level if necessary
$modifiedLogLevel = $this->determineLogLevel($throwable, $record['level']);
$modifiedLogLevel = $this->determineLogLevel($throwable, $level);

/** @phpstan-var Level $modifiedLogLevel */
$record['level'] = $modifiedLogLevel;
$record['level_name'] = Logger::getLevelName($modifiedLogLevel);
if ($level === $modifiedLogLevel->value) {
return $record;
}

return $record;
return $record->with(level: $modifiedLogLevel);
}
}
9 changes: 7 additions & 2 deletions Monolog/Processor/ExtraFieldProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace ETSGlobal\LogBundle\Monolog\Processor;

use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;

/** @internal */
Expand All @@ -13,9 +14,13 @@ public function __construct(private string $fieldName, private string $fieldValu
{
}

public function __invoke(array $record): array
public function __invoke(LogRecord $record): LogRecord
{
$record['extra'][$this->fieldName] = $this->fieldValue;
/** @var array $extra */
$extra = $record['extra'];

$extra[$this->fieldName] = $this->fieldValue;
$record['extra'] = $extra;

return $record;
}
Expand Down
10 changes: 8 additions & 2 deletions Monolog/Processor/TokenCollectionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace ETSGlobal\LogBundle\Monolog\Processor;

use ETSGlobal\LogBundle\Tracing\TokenCollection;
use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;

/** @internal */
Expand All @@ -14,12 +15,17 @@ public function __construct(private TokenCollection $tokenCollection)
{
}

public function __invoke(array $record): array
public function __invoke(LogRecord $record): LogRecord
{
/** @var array $extra */
$extra = $record['extra'];

foreach ($this->tokenCollection->getTokens() as $token) {
$record['extra']['token_' . $token->getName()] = $token->getValue();
$extra['token_' . $token->getName()] = $token->getValue();
}

$record['extra'] = $extra;

return $record;
}
}
30 changes: 22 additions & 8 deletions Tests/Monolog/Formatter/TokenCollectionFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use ETSGlobal\LogBundle\Monolog\Formatter\TokenCollectionFormatter;
use ETSGlobal\LogBundle\Tracing\Token;
use ETSGlobal\LogBundle\Tracing\TokenCollection;
use Monolog\Logger;
use Monolog\LogRecord;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
Expand Down Expand Up @@ -35,10 +37,16 @@ public function testFormatWithoutToken(): void
{
$this->tokenCollection->getTokens()->shouldBeCalled()->willReturn([]);

$this->assertEquals("[%%]\n", $this->tokenCollectionFormatter->format([
'extra' => [],
'context' => [],
]));
$this->assertEquals("[%%]\n", $this->tokenCollectionFormatter->format(
new LogRecord(
new \DateTimeImmutable(),
'chan',
Logger::toMonologLevel(100),
'message',
[],
[],
),
));
}

public function testFormatWithToken(): void
Expand All @@ -47,9 +55,15 @@ public function testFormatWithToken(): void
'tokenA' => new Token('tokenA', 'tokenA_fake_value'),
]);

$this->assertEquals("[%tokenA_fake_value%]\n", $this->tokenCollectionFormatter->format([
'extra' => ['token_tokenA' => 'tokenA_fake_value'],
'context' => [],
]));
$this->assertEquals("[%tokenA_fake_value%]\n", $this->tokenCollectionFormatter->format(
new LogRecord(
new \DateTimeImmutable(),
'chan',
Logger::toMonologLevel(100),
'message',
[],
['token_tokenA' => 'tokenA_fake_value'],
),
));
}
}
Loading