Completed
Push — master ( 4604a2...31c42c )
by Radu
02:05
created

ExceptionLogger   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 4
b 0
f 0
dl 0
loc 23
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A log() 0 11 3
A __construct() 0 5 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework\Log;
6
7
use WebServCo\Framework\Helpers\ErrorMessageHelper;
8
use WebServCo\Framework\Interfaces\LoggerInterface;
9
10
/**
11
* A helper class for logging exceptions to a standard error(CLI) log file.
12
*/
13
class ExceptionLogger
14
{
15
    protected LoggerInterface $fileLogger;
16
17
    public function __construct()
18
    {
19
        $this->fileLogger = new \WebServCo\Framework\Log\FileLogger(
20
            \WebServCo\Framework\Helpers\PhpHelper::isCli() ? 'errorCLI' : 'error',
21
            \WebServCo\Framework\Environment\Config::string('APP_PATH_LOG'),
22
        );
23
    }
24
25
    public function log(\Throwable $exception): void
26
    {
27
        $this->fileLogger->error(ErrorMessageHelper::format($exception), $exception->getTrace());
28
        $previous = $exception->getPrevious();
29
        if (!$previous instanceof \Throwable) {
0 ignored issues
show
introduced by
$previous is always a sub-type of Throwable.
Loading history...
30
            return;
31
        }
32
        do {
33
            $this->fileLogger->error(ErrorMessageHelper::format($previous), $previous->getTrace());
34
        // phpcs:ignore SlevomatCodingStandard.ControlStructures.AssignmentInCondition.AssignmentInCondition
35
        } while ($previous = $previous->getPrevious());
36
    }
37
}
38