1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SKien\XLogger; |
5
|
|
|
|
6
|
|
|
use CCampbell\ChromePhp\ChromePhp; |
7
|
|
|
use Psr\Log\LogLevel; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* PSR-3 compliant logger for Output to the console of the Chrome browser. |
11
|
|
|
* |
12
|
|
|
* This class does not use all of Chrome Logger capabilities, but can be used |
13
|
|
|
* very flexible due to the PSR-3 compatibility components into the browser console. |
14
|
|
|
* |
15
|
|
|
* @package XLogger |
16
|
|
|
* @author Stefanius <[email protected]> |
17
|
|
|
* @copyright MIT License - see the LICENSE file for details |
18
|
|
|
*/ |
19
|
|
|
class ChromePHPLogger extends XLogger |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Init logging level and remote username (if set). |
23
|
|
|
* @see XLogger::setLogLevel() |
24
|
|
|
* @param string $level the min. `LogLevel` to be logged |
25
|
|
|
*/ |
26
|
|
|
public function __construct(string $level = LogLevel::DEBUG) |
27
|
|
|
{ |
28
|
|
|
parent::__construct($level); |
29
|
|
|
// We have to set the backtrace level to 'ignore' last calls inside logger class when |
30
|
|
|
// displaying filename/line of the log call. |
31
|
|
|
ChromePhp::getInstance()->addSetting(ChromePhp::BACKTRACE_LEVEL, 3); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Logs with an arbitrary level. |
36
|
|
|
* @param mixed $level |
37
|
|
|
* @param string $message |
38
|
|
|
* @param mixed[] $context |
39
|
|
|
* @return void |
40
|
|
|
* @throws \Psr\Log\InvalidArgumentException |
41
|
|
|
*/ |
42
|
|
|
public function log($level, $message, array $context = array()) : void |
43
|
|
|
{ |
44
|
|
|
// check, if requested level should be logged |
45
|
|
|
// causes InvalidArgumentException in case of unknown level. |
46
|
|
|
if (!$this->logLevel($level)) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
$strMessage = 'PHP-' . strtoupper($level) . ': ' . $this->replaceContext($message, $context); |
50
|
|
|
switch ($level) { |
51
|
|
|
case LogLevel::EMERGENCY: |
52
|
|
|
case LogLevel::ALERT: |
53
|
|
|
case LogLevel::CRITICAL: |
54
|
|
|
case LogLevel::ERROR: |
55
|
|
|
ChromePhp::error($strMessage); |
56
|
|
|
break; |
57
|
|
|
case LogLevel::WARNING: |
58
|
|
|
ChromePhp::warn($strMessage); |
59
|
|
|
break; |
60
|
|
|
case LogLevel::NOTICE: |
61
|
|
|
ChromePhp::log($strMessage); |
62
|
|
|
break; |
63
|
|
|
case LogLevel::INFO: |
64
|
|
|
ChromePhp::info($strMessage); |
65
|
|
|
break; |
66
|
|
|
case LogLevel::DEBUG: |
67
|
|
|
ChromePhp::log($strMessage); |
68
|
|
|
break; |
69
|
|
|
} |
70
|
|
|
if (count($context) > 0) { |
71
|
|
|
ChromePhp::group(); |
72
|
|
|
foreach ($context as $key => $value) { |
73
|
|
|
// only add, if not included as placeholder in the mesage |
74
|
|
|
if (strpos($message, '{' . $key . '}') === false) { |
75
|
|
|
ChromePhp::info($key, $value); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
ChromePhp::groupEnd(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|