| Total Complexity | 15 |
| Total Lines | 68 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 1 |
| 1 | <?php |
||
| 8 | class BugsnagLogger extends AbstractProcessingHandler |
||
| 9 | { |
||
| 10 | private Bugsnag $bugsnag; |
||
| 11 | |||
| 12 | public function __construct(Bugsnag $client, $level = Logger::WARNING, $bubble = true) |
||
| 16 | } |
||
| 17 | |||
| 18 | |||
| 19 | /** |
||
| 20 | * Function that gets called when a log is getting written. This function will send the log to Bugsnag. |
||
| 21 | * |
||
| 22 | * @param array $record |
||
| 23 | * @return void |
||
| 24 | */ |
||
| 25 | protected function write(array $record) |
||
| 26 | { |
||
| 27 | if (isset($record['context'])) { |
||
| 28 | if (!isset($record['context']['exception'])) { |
||
| 29 | $this->getBugsnag()->sendError($record['message']); |
||
| 30 | return; |
||
| 31 | } |
||
| 32 | //check if $record['context']['exception'] is an instance of \Exception |
||
| 33 | if ($record['context']['exception'] instanceof \Exception) { |
||
| 34 | $this->getBugsnag() |
||
| 35 | ->addUserInfo() |
||
| 36 | ->addPackagesWithVersions() |
||
| 37 | ->sendException( |
||
| 38 | $record['context']['exception'], |
||
| 39 | $this->levelToSeverity($record['level']), |
||
| 40 | true, |
||
| 41 | false |
||
| 42 | ); |
||
| 43 | return; |
||
| 44 | } |
||
| 45 | $this->getBugsnag()->sendError($record['message']); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | public function getBugsnag(): Bugsnag |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Convert Monolog Level to Bugsnag Severity. |
||
| 56 | * |
||
| 57 | * @param int $level |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | private function levelToSeverity($level) |
||
| 76 | } |
||
| 77 | } |
||
| 78 |