Explorar el Código

Merge branch '2.x'

Jordi Boggiano hace 3 años
padre
commit
8471abd5ef

+ 12 - 1
src/Monolog/Handler/BrowserConsoleHandler.php

@@ -15,6 +15,7 @@ use Monolog\Formatter\FormatterInterface;
 use Monolog\Formatter\LineFormatter;
 use Monolog\Utils;
 use Monolog\LogRecord;
+use Monolog\Level;
 
 use function count;
 use function headers_list;
@@ -173,7 +174,7 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
             $extra = self::dump('Extra', $record->extra);
 
             if (\count($context) === 0 && \count($extra) === 0) {
-                $script[] = self::call_array('log', self::handleStyles($record->formatted));
+                $script[] = self::call_array(self::getConsoleMethodForLevel($record->level), self::handleStyles($record->formatted));
             } else {
                 $script = array_merge(
                     $script,
@@ -188,6 +189,16 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
         return "(function (c) {if (c && c.groupCollapsed) {\n" . implode("\n", $script) . "\n}})(console);";
     }
 
+    private static function getConsoleMethodForLevel(Level $level): string
+    {
+        return match ($level) {
+            Level::Debug => 'debug',
+            Level::Info, Level::Notice => 'info',
+            Level::Warning => 'warn',
+            Level::Error, Level::Critical, Level::Alert, Level::Emergency => 'error',
+        };
+    }
+
     /**
      * @return string[]
      */

+ 10 - 10
tests/Monolog/Handler/BrowserConsoleHandlerTest.php

@@ -41,7 +41,7 @@ class BrowserConsoleHandlerTest extends TestCase
 
         $expected = <<<EOF
 (function (c) {if (c && c.groupCollapsed) {
-c.log("%cfoo%cbar%c", "font-weight: normal", "color: red", "font-weight: normal");
+c.debug("%cfoo%cbar%c", "font-weight: normal", "color: red", "font-weight: normal");
 }})(console);
 EOF;
 
@@ -57,7 +57,7 @@ EOF;
 
         $expected = <<<EOF
 (function (c) {if (c && c.groupCollapsed) {
-c.log("%cfoo%cbar%c%cbaz%c", "font-weight: normal", "color: red", "font-weight: normal", "color: blue", "font-weight: normal");
+c.debug("%cfoo%cbar%c%cbaz%c", "font-weight: normal", "color: red", "font-weight: normal", "color: blue", "font-weight: normal");
 }})(console);
 EOF;
 
@@ -73,7 +73,7 @@ EOF;
 
         $expected = <<<EOF
 (function (c) {if (c && c.groupCollapsed) {
-c.log("%c[foo] %c\"bar\\n[baz]\"%c", "font-weight: normal", "color: red", "font-weight: normal");
+c.debug("%c[foo] %c\"bar\\n[baz]\"%c", "font-weight: normal", "color: red", "font-weight: normal");
 }})(console);
 EOF;
 
@@ -91,9 +91,9 @@ EOF;
 
         $expected = <<<EOF
 (function (c) {if (c && c.groupCollapsed) {
-c.log("%c%cfoo%c", "font-weight: normal", "background-color: blue; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
-c.log("%c%cbar%c", "font-weight: normal", "background-color: green; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
-c.log("%c%cfoo%c", "font-weight: normal", "background-color: blue; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
+c.debug("%c%cfoo%c", "font-weight: normal", "background-color: blue; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
+c.debug("%c%cbar%c", "font-weight: normal", "background-color: green; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
+c.debug("%c%cfoo%c", "font-weight: normal", "background-color: blue; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
 }})(console);
 EOF;
 
@@ -135,10 +135,10 @@ EOF;
 
         $expected = <<<EOF
 (function (c) {if (c && c.groupCollapsed) {
-c.log("%ctest1", "font-weight: normal");
-c.log("%ctest2", "font-weight: normal");
-c.log("%ctest3", "font-weight: normal");
-c.log("%ctest4", "font-weight: normal");
+c.debug("%ctest1", "font-weight: normal");
+c.debug("%ctest2", "font-weight: normal");
+c.debug("%ctest3", "font-weight: normal");
+c.debug("%ctest4", "font-weight: normal");
 }})(console);
 EOF;