Completed
Push — master ( fec4ab...528705 )
by Radu
02:19
created

ExceptionLogger::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework\Log;
6
7
use WebServCo\Framework\Interfaces\ConfigInterface;
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 ConfigInterface $configInterface;
16
    protected LoggerInterface $fileLogger;
17
18
    public function __construct(ConfigInterface $configInterface)
19
    {
20
        $this->configInterface = $configInterface;
21
        $this->fileLogger = new \WebServCo\Framework\Log\FileLogger(
22
            \WebServCo\Framework\Helpers\PhpHelper::isCli() ? 'errorCLI' : 'error',
23
            $this->configInterface->get('app/path/log'),
24
        );
25
    }
26
27
    public function getFormattedMessage(\Throwable $exception): string
28
    {
29
        return \sprintf(
30
            'Error: %s in %s:%s.',
31
            $exception->getMessage(),
32
            $exception->getFile(),
33
            $exception->getLine(),
34
        );
35
    }
36
37
    public function log(\Throwable $exception): void
38
    {
39
        $errorMessage = $this->getFormattedMessage($exception);
40
        $errorInfo = \WebServCo\Framework\ErrorHandler::getErrorInfo($exception);
41
        $this->fileLogger->error($errorMessage, $errorInfo);
42
    }
43
}
44