1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-08 14:16:42 +02:00

Add support for Javascript output in BrowserConsoleHandler

This commit is contained in:
Daniel Blok
2015-06-22 15:31:01 +02:00
parent 49f95a2d63
commit f856456b3d

View File

@@ -55,15 +55,19 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
/** /**
* Convert records to javascript console commands and send it to the browser. * Convert records to javascript console commands and send it to the browser.
* This method is automatically called on PHP shutdown if output is HTML. * This method is automatically called on PHP shutdown if output is HTML or Javascript.
*/ */
public static function send() public static function send()
{ {
$htmlTags = true;
// Check content type // Check content type
foreach (headers_list() as $header) { foreach (headers_list() as $header) {
if (stripos($header, 'content-type:') === 0) { if (stripos($header, 'content-type:') === 0) {
if (stripos($header, 'text/html') === false) { // This handler only works with HTML and javascript outputs
// This handler only works with HTML outputs // text/javascript is obsoute in favour of application/javascript, but still used
if (stripos($header, 'application/javascript') !== false || stripos($header, 'text/javascript') !== false) {
$htmlTags = false;
} elseif (stripos($header, 'text/html') === false) {
return; return;
} }
break; break;
@@ -71,7 +75,11 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
} }
if (count(self::$records)) { if (count(self::$records)) {
echo '<script>' , self::generateScript() , '</script>'; if ($htmlTags) {
echo '<script>' , self::generateScript() , '</script>';
} else {
echo self::generateScript();
}
self::reset(); self::reset();
} }
} }