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