mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-14 04:00:57 +03:00
Updated logger to properly format exceptions using processors
This commit is contained in:
parent
9e49604ce2
commit
2eca0da852
3 changed files with 110 additions and 1 deletions
|
@ -1,15 +1,19 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink;
|
||||
|
||||
use Monolog\Handler\RotatingFileHandler;
|
||||
use Monolog\Logger;
|
||||
use Monolog\Processor;
|
||||
use const PHP_EOL;
|
||||
|
||||
return [
|
||||
|
||||
'logger' => [
|
||||
'formatters' => [
|
||||
'dashed' => [
|
||||
'format' => '[%datetime%] %channel%.%level_name% - %message% %context%' . PHP_EOL,
|
||||
'format' => '[%datetime%] %channel%.%level_name% - %message%' . PHP_EOL,
|
||||
'include_stacktraces' => true,
|
||||
],
|
||||
],
|
||||
|
@ -24,9 +28,19 @@ return [
|
|||
],
|
||||
],
|
||||
|
||||
'processors' => [
|
||||
'exception_with_new_line' => [
|
||||
'class' => Common\Logger\Processor\ExceptionWithNewLineProcessor::class,
|
||||
],
|
||||
'psr3' => [
|
||||
'class' => Processor\PsrLogMessageProcessor::class,
|
||||
],
|
||||
],
|
||||
|
||||
'loggers' => [
|
||||
'Shlink' => [
|
||||
'handlers' => ['rotating_file_handler'],
|
||||
'processors' => ['exception_with_new_line', 'psr3'],
|
||||
],
|
||||
],
|
||||
],
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Common\Logger\Processor;
|
||||
|
||||
use const PHP_EOL;
|
||||
use function str_replace;
|
||||
use function strpos;
|
||||
|
||||
final class ExceptionWithNewLineProcessor
|
||||
{
|
||||
private const EXCEPTION_PLACEHOLDER = '{e}';
|
||||
|
||||
public function __invoke(array $record)
|
||||
{
|
||||
$message = $record['message'];
|
||||
$messageHasExceptionPlaceholder = strpos($message, self::EXCEPTION_PLACEHOLDER) !== false;
|
||||
|
||||
if ($messageHasExceptionPlaceholder) {
|
||||
$record['message'] = str_replace(
|
||||
self::EXCEPTION_PLACEHOLDER,
|
||||
PHP_EOL . self::EXCEPTION_PLACEHOLDER,
|
||||
$message
|
||||
);
|
||||
}
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Common\Logger\Processor;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Common\Logger\Processor\ExceptionWithNewLineProcessor;
|
||||
use const PHP_EOL;
|
||||
|
||||
class ExceptionWithNewLineProcessorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ExceptionWithNewLineProcessor
|
||||
*/
|
||||
private $processor;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->processor = new ExceptionWithNewLineProcessor();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideNoPlaceholderRecords
|
||||
*/
|
||||
public function keepsRecordAsIsWhenNoPlaceholderExists(array $record)
|
||||
{
|
||||
$this->assertSame($record, ($this->processor)($record));
|
||||
}
|
||||
|
||||
public function provideNoPlaceholderRecords(): array
|
||||
{
|
||||
return [
|
||||
[['message' => 'Hello World']],
|
||||
[['message' => 'Shlink']],
|
||||
[['message' => 'Foo bar']],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider providePlaceholderRecords
|
||||
*/
|
||||
public function properlyReplacesExceptionPlaceholderAddingNewLine(array $record, array $expected)
|
||||
{
|
||||
$this->assertEquals($expected, ($this->processor)($record));
|
||||
}
|
||||
|
||||
public function providePlaceholderRecords(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
['message' => 'Hello World with placeholder {e}'],
|
||||
['message' => 'Hello World with placeholder ' . PHP_EOL . '{e}'],
|
||||
],
|
||||
[
|
||||
['message' => '{e} Shlink'],
|
||||
['message' => PHP_EOL . '{e} Shlink'],
|
||||
],
|
||||
[
|
||||
['message' => 'Foo {e} bar'],
|
||||
['message' => 'Foo ' . PHP_EOL . '{e} bar'],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue