Completed
Push — master ( 417ea8...dd48e2 )
by Maik
01:38
created

ExceptionLoggerTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 27
ccs 10
cts 11
cp 0.9091
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
logImpl() 0 1 ?
A logException() 0 16 3
1
<?php
2
3
/**
4
 * This file is part of the PHP Generics package.
5
 *
6
 * @package Generics
7
 */
8
namespace Generics\Logger;
9
10
use Psr\Log\LogLevel;
11
use ErrorException;
12
use Exception;
13
use RuntimeException;
14
15
/**
16
 * Implementation for logging exceptions
17
 *
18
 * @author Maik Greubel <[email protected]>
19
 */
20
trait ExceptionLoggerTrait
21
{
22
23
    abstract protected function logImpl($level, $message, array $context = array());
24
25
    /**
26
     *
27
     * {@inheritdoc}
28
     * @see \Generics\Logger\ExceptionLogger::logException()
29
     */
30 3
    public function logException(Exception $ex)
31
    {
32 3
        $level = LogLevel::ALERT;
33
        
34 3
        if ($ex instanceof ErrorException) {
0 ignored issues
show
Bug introduced by
The class ErrorException does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
35 1
            $level = LogLevel::ERROR;
36
        } elseif ($ex instanceof RuntimeException) {
37 1
            $level = LogLevel::EMERGENCY;
38
        }
39
        
40 3
        $this->logImpl($level, "({code}): {message}\n{stackTrace}", array(
41 3
            'code' => $ex->getCode(),
42 3
            'message' => $ex->getMessage(),
43 3
            'stackTrace' => $ex->getTraceAsString()
44
        ));
45 3
    }
46
}
47