Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a4826c4
commit 6391ed7
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace WyriHaximus\PSR3\CallableThrowableLogger; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Throwable; | ||
|
||
final class CallableThrowableLogger | ||
{ | ||
const MESSAGE = 'Uncaught Throwable %s: "%s" at %s line %s'; | ||
|
||
public static function create(LoggerInterface $logger, string $level = 'error'): callable | ||
{ | ||
return function (Throwable $throwable) use ($logger, $level) { | ||
$logger->log( | ||
$level, | ||
\sprintf( | ||
self::MESSAGE, | ||
\get_class($throwable), | ||
$throwable->getMessage(), | ||
$throwable->getFile(), | ||
$throwable->getLine() | ||
), | ||
[ | ||
'throwable' => $throwable, | ||
] | ||
); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace WyriHaximus\Tests\PSR3\CallableThrowableLogger; | ||
|
||
use Exception; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Log\LoggerInterface; | ||
use Throwable; | ||
use WyriHaximus\PSR3\CallableThrowableLogger\CallableThrowableLogger; | ||
|
||
final class CallableThrowableLoggerTest extends TestCase | ||
{ | ||
public function provideThrowables() | ||
{ | ||
yield [new Exception('foo.bar')]; | ||
yield [new CallableThrowableLoggerTestException('bar.foo')]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideThrowables | ||
*/ | ||
public function testCallable(Throwable $throwable) | ||
{ | ||
$logger = $this->prophesize(LoggerInterface::class); | ||
$logger->log( | ||
'error', | ||
\sprintf( | ||
CallableThrowableLogger::MESSAGE, | ||
\get_class($throwable), | ||
$throwable->getMessage(), | ||
$throwable->getFile(), | ||
$throwable->getLine() | ||
), | ||
[ | ||
'throwable' => $throwable, | ||
] | ||
)->shouldBeCalled(); | ||
|
||
$callable = CallableThrowableLogger::create($logger->reveal()); | ||
$callable($throwable); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace WyriHaximus\Tests\PSR3\CallableThrowableLogger; | ||
|
||
use Exception; | ||
|
||
final class CallableThrowableLoggerTestException extends Exception | ||
{ | ||
} |