37 return new LineFormatter(
'[[%channel%]]{macro: autolabel} [[%level_name%]]{font-weight: bold} %message%');
49 if (!self::$initialized) {
50 self::$initialized =
true;
59 public static function send()
61 $format = self::getResponseFormat();
62 if ($format ===
'unknown') {
67 if ($format ===
'html') {
68 self::writeOutput(
'<script>' . self::generateScript() .
'</script>');
69 } elseif ($format ===
'js') {
70 self::writeOutput(self::generateScript());
89 if (PHP_SAPI !==
'cli') {
90 register_shutdown_function(
array(
'Monolog\Handler\BrowserConsoleHandler',
'send'));
116 foreach (headers_list() as
$header) {
117 if (stripos($header,
'content-type:') === 0) {
120 if (stripos($header,
'application/javascript') !==
false || stripos($header,
'text/javascript') !==
false) {
123 if (stripos($header,
'text/html') ===
false) {
137 $context = self::dump(
'Context', $record[
'context']);
138 $extra = self::dump(
'Extra', $record[
'extra']);
140 if (empty($context) && empty($extra)) {
141 $script[] = self::call_array(
'log', self::handleStyles($record[
'formatted']));
143 $script = array_merge($script,
144 array(self::call_array(
'groupCollapsed', self::handleStyles($record[
'formatted']))),
147 array(self::call(
'groupEnd'))
152 return "(function (c) {if (c && c.groupCollapsed) {\n" . implode(
"\n", $script) .
"\n}})(console);";
157 $args =
array(self::quote(
'font-weight: normal'));
158 $format =
'%c' . $formatted;
159 preg_match_all(
'/\[\[(.*?)\]\]\{([^}]*)\}/s', $format, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
161 foreach (array_reverse($matches) as $match) {
162 $args[] = self::quote(self::handleCustomStyles($match[2][0], $match[1][0]));
163 $args[] =
'"font-weight: normal"';
166 $format = substr($format, 0, $pos) .
'%c' . $match[1][0] .
'%c' . substr($format, $pos + strlen($match[0][0]));
169 array_unshift($args, self::quote($format));
176 static $colors =
array(
'blue',
'green',
'red',
'magenta',
'orange',
'black',
'grey');
177 static $labels =
array();
179 return preg_replace_callback(
'/macro\s*:(.*?)(?:;|$)/',
function ($m) use ($string, &$colors, &$labels) {
180 if (trim($m[1]) ===
'autolabel') {
182 if (!isset($labels[$string])) {
183 $labels[$string] = $colors[count($labels) % count($colors)];
185 $color = $labels[$string];
187 return "background-color: $color; color: white; border-radius: 3px; padding: 0 2px 0 2px";
197 $dict = array_filter($dict);
201 $script[] = self::call(
'log', self::quote(
'%c%s'), self::quote(
'font-weight: bold'), self::quote(
$title));
202 foreach ($dict as $key => $value) {
203 $value = json_encode($value);
205 $value = self::quote(
'');
207 $script[] = self::call(
'log', self::quote(
'%s: %o'), self::quote($key), $value);
215 return '"' . addcslashes($arg,
"\"\n\\") .
'"';
220 $args = func_get_args();
221 $method = array_shift($args);
223 return self::call_array($method, $args);
228 return 'c.' . $method .
'(' . implode(
', ', $args) .
');';
static getResponseFormat()
Checks the format of the response.
static dump($title, array $dict)
static writeOutput($str)
Wrapper for echo to allow overriding.
Base Handler class providing the Handler structure.
static send()
Convert records to javascript console commands and send it to the browser.
Handler sending logs to browser's javascript console with no browser extension required.
registerShutdownFunction()
Wrapper for register_shutdown_function to allow overriding.
static reset()
Forget all logged records.
static handleStyles($formatted)
Create styles array
The data for the language used.
static handleCustomStyles($style, $string)
static call_array($method, array $args)