Passed
Branch master (fc5382)
by Fabian
03:13
created

Logger   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 16
eloc 36
dl 0
loc 79
ccs 0
cts 36
cp 0
rs 10
c 4
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
C add() 0 48 13
A setDesiredLevel() 0 2 1
A __construct() 0 1 1
A setPSRLogger() 0 2 1
1
<?php
2
3
namespace LE_ACME2\Utilities;
4
5
use LE_ACME2\SingletonTrait;
6
7
class Logger {
8
9
    use SingletonTrait;
10
11
    const LEVEL_DISABLED = 0;
12
    const LEVEL_INFO = 1;
13
    const LEVEL_DEBUG = 2;
14
15
    private function __construct() {}
16
17
    protected $_desiredLevel = self::LEVEL_DISABLED;
18
19
    public function setDesiredLevel(int $desiredLevel) {
20
        $this->_desiredLevel = $desiredLevel;
21
    }
22
23
    /** @var \Psr\Log\LoggerInterface|null $_psrLogger */
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
    private $_psrLogger = null;
25
26
    /**
27
     * @param \Psr\Log\LoggerInterface|null $psrLogger
28
     */
29
    public function setPSRLogger($psrLogger) {
30
        $this->_psrLogger = $psrLogger;
31
    }
32
33
    /**
34
     * @param int $level
35
     * @param string $message
36
     * @param array $data
37
     */
38
    public function add(int $level, string $message, array $data = array()) {
39
40
        if($level > $this->_desiredLevel)
41
            return;
42
43
        if($this->_psrLogger) {
44
45
            if($level == self::LEVEL_INFO) {
46
                $this->_psrLogger->info($message, $data);
47
                return;
48
            }
49
            if($level == self::LEVEL_DEBUG) {
50
                $this->_psrLogger->debug($message, $data);
51
                return;
52
            }
53
            throw new \RuntimeException('Missing PSR Logger support for level: ' . $level);
54
        }
55
56
        $e = new \Exception();
57
        $trace = $e->getTrace();
58
        unset($trace[0]);
59
60
        $output = '<b>' . date('d-m-Y H:i:s') . ': ' . $message . '</b><br>' . "\n";
61
62
        if($this->_desiredLevel == self::LEVEL_DEBUG) {
63
64
            $step = 0;
65
            foreach ($trace as $traceItem) {
66
67
                if(!isset($traceItem['class']) || !isset($traceItem['function'])) {
68
                    continue;
69
                }
70
71
                $output .= 'Trace #' . $step . ': ' . $traceItem['class'] . '::' . $traceItem['function'] . '<br/>' . "\n";
72
                $step++;
73
            }
74
75
            if ((is_array($data) && count($data) > 0) || !is_array($data))
76
                $output .= "\n" .'<br/>Data:<br/>' . "\n" . '<pre>' . var_export($data, true) . '</pre>';
77
78
            $output .= '<br><br>' . "\n\n";
79
        }
80
81
        if(PHP_SAPI == 'cli') {
82
83
            $output = strip_tags($output);
84
        }
85
        echo $output;
86
    }
87
}