38 return new LineFormatter(
'[[%channel%]]{macro: autolabel} [[%level_name%]]{font-weight: bold} %message%');
44 protected function write(array $record)
50 if (PHP_SAPI !==
'cli' && !self::$initialized) {
51 self::$initialized =
true;
52 register_shutdown_function(array(
'Monolog\Handler\BrowserConsoleHandler',
'send'));
60 public static function send()
64 foreach (headers_list() as
$header) {
65 if (stripos(
$header,
'content-type:') === 0) {
68 if (stripos(
$header,
'application/javascript') !==
false || stripos(
$header,
'text/javascript') !==
false) {
70 } elseif (stripos(
$header,
'text/html') ===
false) {
99 $context =
self::dump(
'Context', $record[
'context']);
100 $extra =
self::dump(
'Extra', $record[
'extra']);
102 if (empty($context) && empty($extra)) {
103 $script[] =
self::call_array(
'log', self::handleStyles($record[
'formatted']));
105 $script = array_merge($script,
106 array(self::call_array(
'groupCollapsed', self::handleStyles($record[
'formatted']))),
109 array(self::call(
'groupEnd'))
114 return "(function (c) {if (c && c.groupCollapsed) {\n" . implode(
"\n", $script) .
"\n}})(console);";
119 $args = array(self::quote(
'font-weight: normal'));
120 $format =
'%c' . $formatted;
121 preg_match_all(
'/\[\[(.*?)\]\]\{([^}]*)\}/s', $format, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
123 foreach (array_reverse($matches) as $match) {
124 $args[] =
self::quote(self::handleCustomStyles($match[2][0], $match[1][0]));
125 $args[] =
'"font-weight: normal"';
128 $format = substr($format, 0, $pos) .
'%c' . $match[1][0] .
'%c' . substr($format, $pos + strlen($match[0][0]));
131 array_unshift($args, self::quote($format));
138 static $colors = array(
'blue',
'green',
'red',
'magenta',
'orange',
'black',
'grey');
139 static $labels = array();
141 return preg_replace_callback(
'/macro\s*:(.*?)(?:;|$)/',
function ($m) use ($string, &$colors, &$labels) {
142 if (trim($m[1]) ===
'autolabel') {
144 if (!isset($labels[$string])) {
145 $labels[$string] = $colors[count($labels) % count($colors)];
147 $color = $labels[$string];
149 return "background-color: $color; color: white; border-radius: 3px; padding: 0 2px 0 2px";
156 private static function dump($title, array $dict)
159 $dict = array_filter($dict);
163 $script[] =
self::call(
'log', self::quote(
'%c%s'), self::quote(
'font-weight: bold'), self::quote($title));
164 foreach ($dict as $key => $value) {
165 $value = json_encode($value);
169 $script[] =
self::call(
'log', self::quote(
'%s: %o'), self::quote($key), $value);
177 return '"' . addcslashes($arg,
"\"\n\\") .
'"';
182 $args = func_get_args();
183 $method = array_shift($args);
190 return 'c.' . $method .
'(' . implode(
', ', $args) .
');';
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)
static reset()
Forget all logged records.
static dump($title, array $dict)
getDefaultFormatter()
Gets the default formatter.FormatterInterface
static handleCustomStyles($style, $string)
static handleStyles($formatted)
write(array $record)
Writes the record down to the log of the implementing handler.void
static send()
Convert records to javascript console commands and send it to the browser.