From 2eca0da8527ae214e1377cfc1a4d8a4ca6b66ccb Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 20 Oct 2018 12:37:26 +0200 Subject: [PATCH] Updated logger to properly format exceptions using processors --- config/autoload/logger.global.php | 16 ++++- .../ExceptionWithNewLineProcessor.php | 29 ++++++++ .../ExceptionWithNewLineProcessorTest.php | 66 +++++++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 module/Common/src/Logger/Processor/ExceptionWithNewLineProcessor.php create mode 100644 module/Common/test/Logger/Processor/ExceptionWithNewLineProcessorTest.php diff --git a/config/autoload/logger.global.php b/config/autoload/logger.global.php index 89210706..363e9210 100644 --- a/config/autoload/logger.global.php +++ b/config/autoload/logger.global.php @@ -1,15 +1,19 @@ [ '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'], ], ], ], diff --git a/module/Common/src/Logger/Processor/ExceptionWithNewLineProcessor.php b/module/Common/src/Logger/Processor/ExceptionWithNewLineProcessor.php new file mode 100644 index 00000000..0d48b5c4 --- /dev/null +++ b/module/Common/src/Logger/Processor/ExceptionWithNewLineProcessor.php @@ -0,0 +1,29 @@ +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'], + ], + ]; + } +}