From df545ce931b32d340e40831d0c3afbc6eabda29d Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Wed, 22 Apr 2020 22:35:39 +0200 Subject: fix: Logger::exception didn't print the error position --- app/class/Logger.php | 2 +- tests/LoggerTest.php | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/app/class/Logger.php b/app/class/Logger.php index 5d2695c..104e23a 100644 --- a/app/class/Logger.php +++ b/app/class/Logger.php @@ -95,7 +95,7 @@ class Logger public static function exception(Throwable $e, bool $withtrace = false) { if (self::$verbosity > 0) { - $msg = $e->getMessage(); + $msg = "{$e->getMessage()} in {$e->getFile()}({$e->getLine()})"; if ($withtrace) { // TODO: Maybe print a more beautiful stack trace. $msg .= PHP_EOL . $e->getTraceAsString(); diff --git a/tests/LoggerTest.php b/tests/LoggerTest.php index 95d6cae..b4b87f4 100644 --- a/tests/LoggerTest.php +++ b/tests/LoggerTest.php @@ -242,21 +242,23 @@ class LoggerTest extends FilesTest * @test * @dataProvider exceptionLoggedProvider */ - public function exceptionLoggedTest(int $verbosity, Throwable $e, string $expected) + public function exceptionLoggedTest(int $verbosity, Throwable $e, string $expected, int $line) { Logger::init($this->logfile, $verbosity); Logger::exception($e); - $expected = " [ ERROR ] tests/LoggerTest.php(248) $expected\n"; + $file = __FILE__; + $line += 258; + $expected = " [ ERROR ] tests/LoggerTest.php(248) $expected in $file($line)\n"; $this->assertEquals($expected, substr(file_get_contents($this->logfile), 25)); } public function exceptionLoggedProvider(): array { return [ - [1, new Exception('Test 1'), 'Test 1'], - [2, new Exception('Test 2'), 'Test 2'], - [3, new Exception('Test 3'), 'Test 3'], - [4, new Exception('Test 4'), 'Test 4'], + [1, new Exception('Test 1'), 'Test 1', 0], + [2, new Exception('Test 2'), 'Test 2', 1], + [3, new Exception('Test 3'), 'Test 3', 2], + [4, new Exception('Test 4'), 'Test 4', 3], ]; } @@ -268,8 +270,8 @@ class LoggerTest extends FilesTest Logger::init($this->logfile, 1); Logger::exception(new Exception('Error'), true); $content = file_get_contents($this->logfile); - $expected = " [ ERROR ] tests/LoggerTest.php(269) Error\n"; + $expected = " [ ERROR ] tests/LoggerTest.php(271) Error "; $this->assertEquals($expected, substr($content, 25, 43)); - $this->assertRegExp('/(#\d+ [\w\/\.]*\(\d+\): .*\)\n)+#\d+ \{main\}\n/U', substr($content, 68)); + $this->assertRegExp('/(#\d+ [\w\/\.]*\(\d+\): .*\)\n)+#\d+ \{main\}\n/U', $content); } } -- cgit v1.2.3