|
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\DefaultFormatter; |
|
20
|
|
|
use Phossa2\Logger\Formatter\FormatterInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* BrowserHandler |
|
24
|
|
|
* |
|
25
|
|
|
* Send logs to browser console. |
|
26
|
|
|
* |
|
27
|
|
|
* - User MUST add HTTP header 'browerhandler' to html page to works with |
|
28
|
|
|
* this handler. |
|
29
|
|
|
* |
|
30
|
|
|
* - Modified from Monolog Handler\BrowserConsoleHandler |
|
31
|
|
|
* |
|
32
|
|
|
* - Message like '[[text goes here]]{background-color: green; color: white}' |
|
33
|
|
|
* |
|
34
|
|
|
* @package Phossa2\Logger |
|
35
|
|
|
* @author Hong Zhang <[email protected]> |
|
36
|
|
|
* @see HandlerAbstract |
|
37
|
|
|
* @version 2.0.0 |
|
38
|
|
|
* @since 2.0.0 added |
|
39
|
|
|
*/ |
|
40
|
|
|
class BrowserHandler extends HandlerAbstract |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* cached messages |
|
44
|
|
|
* |
|
45
|
|
|
* @static |
|
46
|
|
|
* @var string[] |
|
47
|
|
|
* @access protected |
|
48
|
|
|
*/ |
|
49
|
|
|
protected static $messages = []; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritDoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct( |
|
55
|
|
|
/*# string */ $level = LogLevel::NOTICE, |
|
56
|
|
|
FormatterInterface $formatter = null, |
|
57
|
|
|
/*# bool */ $stopPropagation = false |
|
58
|
|
|
) { |
|
59
|
|
|
// skip CLI mode |
|
60
|
|
|
if ($this->isCliMode()) { |
|
61
|
|
|
return; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// register flush |
|
65
|
|
|
register_shutdown_function([__CLASS__, 'flush']); |
|
66
|
|
|
|
|
67
|
|
|
parent::__construct($level, $formatter, $stopPropagation); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritDoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function write(LogEntryInterface $logEntry) |
|
74
|
|
|
{ |
|
75
|
|
|
static::$messages[] = $logEntry->getFormatted(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Only use this handler in browser mode |
|
80
|
|
|
* |
|
81
|
|
|
* {@inheritDoc} |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function isHandlingOther(LogEntryInterface $logEntry)/*# : bool */ |
|
84
|
|
|
{ |
|
85
|
|
|
return !$this->isCliMode(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* flush the messages to browser by adding to HTML page |
|
90
|
|
|
* |
|
91
|
|
|
* @return void |
|
92
|
|
|
* @access public |
|
93
|
|
|
* @static |
|
94
|
|
|
* @api |
|
95
|
|
|
*/ |
|
96
|
|
|
public static function flush() |
|
97
|
|
|
{ |
|
98
|
|
|
if (static::hasHttpHeader() && count(static::$messages)) { |
|
99
|
|
|
echo '<script>' , static::generateScript() , '</script>'; |
|
100
|
|
|
static::$messages = []; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Overrided |
|
106
|
|
|
* |
|
107
|
|
|
* Format your message like |
|
108
|
|
|
* |
|
109
|
|
|
* [[text goes here]]{background-color: green; color: white} |
|
110
|
|
|
* |
|
111
|
|
|
* {@inheritDoc} |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getFormatter()/*# : callable */ |
|
114
|
|
|
{ |
|
115
|
|
|
if (is_null($this->formatter)) { |
|
116
|
|
|
$this->formatter = new DefaultFormatter( |
|
117
|
|
|
'[[%channel%]]{macro: autolabel} [[%level_name%]]{font-weight: bold} %message%' |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
return $this->formatter; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Is 'browserhandler' header set ? |
|
125
|
|
|
* |
|
126
|
|
|
* @return bool |
|
127
|
|
|
* @access protected |
|
128
|
|
|
* @static |
|
129
|
|
|
*/ |
|
130
|
|
|
protected static function hasHttpHeader()/*# : bool */ |
|
131
|
|
|
{ |
|
132
|
|
|
foreach (headers_list() as $header) { |
|
133
|
|
|
if (false !== stripos($header, 'browserhandler')) { |
|
134
|
|
|
return true; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
return false; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Generate the javascript |
|
142
|
|
|
* |
|
143
|
|
|
* @return string |
|
144
|
|
|
* @access protected |
|
145
|
|
|
*/ |
|
146
|
|
|
protected static function generateScript()/*# : string */ |
|
147
|
|
|
{ |
|
148
|
|
|
$script = array(); |
|
149
|
|
|
foreach (static::$messages as $record) { |
|
150
|
|
|
$script[] = static::consoleLog(static::handleStyles($record)); |
|
151
|
|
|
} |
|
152
|
|
|
return |
|
153
|
|
|
"(function (c) {if (c && c.groupCollapsed) {\n" . |
|
154
|
|
|
implode("\n", $script) . |
|
155
|
|
|
"\n}})(console);"; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* JS log method |
|
160
|
|
|
* |
|
161
|
|
|
* @param array $args |
|
162
|
|
|
* @return string |
|
163
|
|
|
* @access protected |
|
164
|
|
|
*/ |
|
165
|
|
|
protected static function consoleLog(array $args)/*# : string */ |
|
166
|
|
|
{ |
|
167
|
|
|
return 'c.log(' . implode(', ', $args) . ');'; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Format with styles in the message |
|
172
|
|
|
* |
|
173
|
|
|
* @param string $formatted |
|
174
|
|
|
* @return array |
|
175
|
|
|
* @access protected |
|
176
|
|
|
*/ |
|
177
|
|
|
protected static function handleStyles( |
|
178
|
|
|
/*# : string */ $formatted |
|
179
|
|
|
)/*# : array */ { |
|
180
|
|
|
$args = array(static::quote('font-weight: normal')); |
|
181
|
|
|
$format = '%c' . $formatted; |
|
182
|
|
|
preg_match_all( |
|
183
|
|
|
'/\[\[(.*?)\]\]\{([^}]*)\}/s', |
|
184
|
|
|
$format, |
|
185
|
|
|
$matches, |
|
186
|
|
|
PREG_OFFSET_CAPTURE | PREG_SET_ORDER |
|
187
|
|
|
); |
|
188
|
|
|
foreach (array_reverse($matches) as $match) { |
|
189
|
|
|
$args[] = static::quote( |
|
190
|
|
|
static::handleCustomStyles($match[2][0], $match[1][0]) |
|
191
|
|
|
); |
|
192
|
|
|
$args[] = '"font-weight: normal"'; |
|
193
|
|
|
$pos = $match[0][1]; |
|
194
|
|
|
$format = |
|
195
|
|
|
substr($format, 0, $pos) . '%c' . $match[1][0] . |
|
196
|
|
|
'%c' . substr($format, $pos + strlen($match[0][0])); |
|
197
|
|
|
} |
|
198
|
|
|
array_unshift($args, static::quote($format)); |
|
199
|
|
|
|
|
200
|
|
|
return $args; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @param string $style |
|
205
|
|
|
* @param string $string |
|
206
|
|
|
* @return string |
|
207
|
|
|
* @access protected |
|
208
|
|
|
*/ |
|
209
|
|
|
protected static function handleCustomStyles( |
|
210
|
|
|
/*# string */ $style, |
|
211
|
|
|
/*# string */ $string |
|
212
|
|
|
)/*# : string */ { |
|
213
|
|
|
static $colors = array( |
|
214
|
|
|
'blue', 'green', 'red', 'magenta', 'orange', 'black', 'grey' |
|
215
|
|
|
); |
|
216
|
|
|
static $labels = array(); |
|
217
|
|
|
|
|
218
|
|
|
return preg_replace_callback( |
|
219
|
|
|
'/macro\s*:(.*?)(?:;|$)/', |
|
220
|
|
|
function ($mstr) use ($string, &$colors, &$labels) { |
|
221
|
|
|
if (trim($mstr[1]) === 'autolabel') { |
|
222
|
|
|
// Format the string as a label with consistent |
|
223
|
|
|
// auto assigned background color |
|
224
|
|
|
if (!isset($labels[$string])) { |
|
225
|
|
|
$labels[$string] = $colors[count($labels) % count($colors)]; |
|
226
|
|
|
} |
|
227
|
|
|
$color = $labels[$string]; |
|
228
|
|
|
return "background-color: $color; color: white; border-radius: 3px; padding: 0 2px 0 2px"; |
|
229
|
|
|
} |
|
230
|
|
|
return $mstr[1]; |
|
231
|
|
|
}, |
|
232
|
|
|
$style |
|
233
|
|
|
); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Quote $arg |
|
238
|
|
|
* |
|
239
|
|
|
* @param string $arg |
|
240
|
|
|
* @return string |
|
241
|
|
|
* @access protected |
|
242
|
|
|
*/ |
|
243
|
|
|
protected static function quote(/*# string */ $arg)/*# : string */ |
|
244
|
|
|
{ |
|
245
|
|
|
return '"' . addcslashes($arg, "\"\n\\") . '"'; |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|