1 | <?php |
||
29 | class ErrorLogger implements \Psr\Log\LoggerAwareInterface |
||
30 | { |
||
31 | use \Psr\Log\LoggerAwareTrait; |
||
32 | |||
33 | /** |
||
34 | * @var array<string,string> Stores the Exception class name to log level |
||
35 | */ |
||
36 | private $levels; |
||
37 | |||
38 | /** |
||
39 | * Creates a new ErrorLogger. |
||
40 | * |
||
41 | * If an exception isn't found in the `$levels` map, this class assumes a |
||
42 | * level of `LogLevel::ERROR`. |
||
43 | * |
||
44 | * ```php |
||
45 | * $elog = new ErrorLogger( |
||
46 | * $logger, |
||
47 | * ["MyException" => LogLevel::DEBUG, "RuntimeException" => LogLevel::WARN] |
||
48 | * ); |
||
49 | * ``` |
||
50 | * |
||
51 | * @param $logger - The logger; will use `Psr\Log\NullLogger` by default |
||
52 | * @param $levels - Map of Exception names to log levels. Order matters! |
||
53 | */ |
||
54 | 1 | public function __construct(Logger $logger = null, array $levels = null) |
|
55 | { |
||
56 | 1 | $this->logger = $logger ?? new \Psr\Log\NullLogger(); |
|
57 | $this->levels = $levels === null ? [] : array_map(function ($a) { |
||
58 | return (string) $a; |
||
59 | }, $levels); |
||
60 | 1 | } |
|
61 | |||
62 | /** |
||
63 | * Logs an exception. |
||
64 | * |
||
65 | * If an exception isn't found in the `$levels` map, this class assumes a |
||
66 | * level of `LogLevel::ERROR`. |
||
67 | * |
||
68 | * @param $e - The exception to log |
||
69 | */ |
||
70 | 1 | public function log(\Exception $e) |
|
81 | } |
||
82 |