37 return new LineFormatter(
'[[%channel%]]{macro: autolabel} [[%level_name%]]{font-weight: bold} %message%');
43 protected function write(array $record)
49 if (!static::$initialized) {
50 static::$initialized =
true;
59 public static function send()
61 $format = static::getResponseFormat();
68 static::writeOutput(
'<script>' . static::generateScript() .
'</script>');
70 static::writeOutput(static::generateScript());
72 static::resetStatic();
99 if (PHP_SAPI !==
'cli') {
100 register_shutdown_function(array(
'Monolog\Handler\BrowserConsoleHandler',
'send'));
126 foreach (headers_list() as
$header) {
127 if (stripos(
$header,
'content-type:') === 0) {
130 if (stripos(
$header,
'application/javascript') !==
false || stripos(
$header,
'text/javascript') !==
false) {
133 if (stripos(
$header,
'text/html') ===
false) {
147 $context = static::dump(
'Context', $record[
'context']);
148 $extra = static::dump(
'Extra', $record[
'extra']);
150 if (empty(
$context) && empty($extra)) {
151 $script[] = static::call_array(
'log', static::handleStyles($record[
'formatted']));
153 $script = array_merge($script,
154 array(static::call_array(
'groupCollapsed', static::handleStyles($record[
'formatted']))),
157 array(static::call(
'groupEnd'))
162 return "(function (c) {if (c && c.groupCollapsed) {\n" . implode(
"\n", $script) .
"\n}})(console);";
167 $args = array(static::quote(
'font-weight: normal'));
169 preg_match_all(
'/\[\[(.*?)\]\]\{([^}]*)\}/s',
$format, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
171 foreach (array_reverse($matches) as $match) {
172 $args[] = static::quote(static::handleCustomStyles($match[2][0], $match[1][0]));
173 $args[] =
'"font-weight: normal"';
176 $format = substr(
$format, 0, $pos) .
'%c' . $match[1][0] .
'%c' . substr(
$format, $pos + strlen($match[0][0]));
179 array_unshift($args, static::quote(
$format));
186 static $colors = array(
'blue',
'green',
'red',
'magenta',
'orange',
'black',
'grey');
187 static $labels = array();
189 return preg_replace_callback(
'/macro\s*:(.*?)(?:;|$)/',
function (
$m) use ($string, &$colors, &$labels) {
190 if (trim(
$m[1]) ===
'autolabel') {
192 if (!isset($labels[$string])) {
193 $labels[$string] = $colors[count($labels) % count($colors)];
195 $color = $labels[$string];
197 return "background-color: $color; color: white; border-radius: 3px; padding: 0 2px 0 2px";
207 $dict = array_filter($dict);
211 $script[] = static::call(
'log', static::quote(
'%c%s'), static::quote(
'font-weight: bold'), static::quote(
$title));
212 foreach ($dict as
$key => $value) {
213 $value = json_encode($value);
215 $value = static::quote(
'');
217 $script[] = static::call(
'log', static::quote(
'%s: %o'), static::quote(
$key), $value);
225 return '"' . addcslashes($arg,
"\"\n\\") .
'"';
230 $args = func_get_args();
231 $method = array_shift($args);
233 return static::call_array($method, $args);
238 return 'c.' . $method .
'(' . implode(
', ', $args) .
');';
An exception for terminatinating execution or to throw for unit testing.
Base Handler class providing the Handler structure.
Handler sending logs to browser's javascript console with no browser extension required.
static call_array($method, array $args)
close()
Closes the handler.
static dump($title, array $dict)
getDefaultFormatter()
Gets the default formatter.FormatterInterface
registerShutdownFunction()
Wrapper for register_shutdown_function to allow overriding.
static handleCustomStyles($style, $string)
static resetStatic()
Forget all logged records.
static handleStyles($formatted)
write(array $record)
Writes the record down to the log of the implementing handler.void
static writeOutput($str)
Wrapper for echo to allow overriding.
static send()
Convert records to javascript console commands and send it to the browser.
static getResponseFormat()
Checks the format of the response.