1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Logger |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Logger\Handler; |
16
|
|
|
|
17
|
|
|
use Phossa2\Logger\LogLevel; |
18
|
|
|
use Phossa2\Logger\Entry\LogEntryInterface; |
19
|
|
|
use Phossa2\Logger\Formatter\FormatterInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* BrowserHandler |
23
|
|
|
* |
24
|
|
|
* Send logs to browser console. |
25
|
|
|
* |
26
|
|
|
* - User MUST add HTTP header 'browerhandler' to html page to works with |
27
|
|
|
* this handler. |
28
|
|
|
* |
29
|
|
|
* - Modified from Monolog Handler\BrowserConsoleHandler |
30
|
|
|
* |
31
|
|
|
* @package Phossa2\Logger |
32
|
|
|
* @author Hong Zhang <[email protected]> |
33
|
|
|
* @see HandlerAbstract |
34
|
|
|
* @version 2.0.0 |
35
|
|
|
* @since 2.0.0 added |
36
|
|
|
*/ |
37
|
|
|
class BrowserHandler extends HandlerAbstract |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* cached messages |
41
|
|
|
* |
42
|
|
|
* @static |
43
|
|
|
* @var string[] |
44
|
|
|
* @access protected |
45
|
|
|
*/ |
46
|
|
|
protected static $messages = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
/*# string */ $level = LogLevel::DEBUG, |
53
|
|
|
FormatterInterface $formatter = null, |
54
|
|
|
/*# bool */ $stopPropagation = false |
55
|
|
|
) { |
56
|
|
|
// skip CLI mode |
57
|
|
|
if ($this->isCliMode()) { |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// register flush |
62
|
|
|
register_shutdown_function([__CLASS__, 'flush']); |
63
|
|
|
|
64
|
|
|
parent::__construct($level, $formatter, $stopPropagation); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritDoc} |
69
|
|
|
*/ |
70
|
|
|
protected function write(LogEntryInterface $logEntry) |
71
|
|
|
{ |
72
|
|
|
static::$messages[] = $logEntry->getFormatted(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Only use this handler in browser mode |
77
|
|
|
* |
78
|
|
|
* {@inheritDoc} |
79
|
|
|
*/ |
80
|
|
|
protected function isHandlingOther(LogEntryInterface $logEntry)/*# : bool */ |
81
|
|
|
{ |
82
|
|
|
return !$this->isCliMode(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* flush the messages to browser by adding to HTML page |
87
|
|
|
* |
88
|
|
|
* @return void |
89
|
|
|
* @access public |
90
|
|
|
* @static |
91
|
|
|
* @api |
92
|
|
|
*/ |
93
|
|
|
public static function flush() |
94
|
|
|
{ |
95
|
|
|
if (static::hasHttpHeader() && count(static::$messages)) { |
96
|
|
|
echo '<script>' , static::generateScript() , '</script>'; |
97
|
|
|
} |
98
|
|
|
static::$messages = []; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Is 'browserhandler' header set ? |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
* @access protected |
106
|
|
|
* @static |
107
|
|
|
*/ |
108
|
|
|
protected static function hasHttpHeader()/*# : bool */ |
109
|
|
|
{ |
110
|
|
|
foreach (headers_list() as $header) { |
111
|
|
|
if (false !== stripos($header, 'browserhandler')) { |
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Generate the javascript |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
* @access protected |
123
|
|
|
*/ |
124
|
|
|
protected static function generateScript()/*# : string */ |
125
|
|
|
{ |
126
|
|
|
$script = array(); |
127
|
|
|
foreach (static::$messages as $msg) { |
128
|
|
|
$script[] = 'c.log(' . $msg . ');'; |
129
|
|
|
} |
130
|
|
|
return "(function (c) {if (c && c.groupCollapsed) {\n" . |
131
|
|
|
implode("\n", $script) . "\n}})(console);"; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|