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