-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPsrLogExceptionReporter.php
More file actions
59 lines (50 loc) · 1.4 KB
/
PsrLogExceptionReporter.php
File metadata and controls
59 lines (50 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/*
* Copyright 2025 Cloud Creativity Limited
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
declare(strict_types=1);
namespace CloudCreativity\Modules\Infrastructure\ExceptionReporter;
use CloudCreativity\Modules\Contracts\Application\Ports\Driven\ExceptionReporter;
use CloudCreativity\Modules\Contracts\Toolkit\Loggable\ContextProvider;
use Psr\Log\LoggerInterface;
use Throwable;
final readonly class PsrLogExceptionReporter implements ExceptionReporter
{
/**
* PsrLogExceptionReporter constructor.
*
* @param LoggerInterface $logger
*/
public function __construct(private LoggerInterface $logger)
{
}
/**
* @inheritDoc
*/
public function report(Throwable $ex): void
{
$this->logger->error(
$ex->getMessage() ?: 'Unexpected error: ' . $ex::class,
[...$this->context($ex), 'exception' => $ex],
);
}
/**
* @param Throwable $ex
* @return array<array-key, mixed>
*/
private function context(Throwable $ex): array
{
if ($ex instanceof ContextProvider) {
return $ex->context();
}
if (method_exists($ex, 'context')) {
$context = $ex->context();
return is_array($context) ? $context : [];
}
return [];
}
}