Skip to content

Commit

Permalink
Update BrowserConsoleHandler console output method (#1739)
Browse files Browse the repository at this point in the history
Instead of using console.log for all log levels, it uses debug, info, warn and error methods depending on the log level.
This makes filtering logs easier in the browser console and highlights error level messages.
  • Loading branch information
pafernandez-oesia committed Aug 2, 2022
1 parent 7204886 commit 620dca1
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Monolog/Handler/BrowserConsoleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Utils;
use Monolog\Logger;

use function count;
use function headers_list;
Expand Down Expand Up @@ -177,7 +178,7 @@ private static function generateScript(): string
$extra = static::dump('Extra', $record['extra']);

if (empty($context) && empty($extra)) {
$script[] = static::call_array('log', static::handleStyles($record['formatted']));
$script[] = static::call_array(static::getConsoleMethodForLevel($record['level']), static::handleStyles($record['formatted']));
} else {
$script = array_merge(
$script,
Expand All @@ -192,6 +193,20 @@ private static function generateScript(): string
return "(function (c) {if (c && c.groupCollapsed) {\n" . implode("\n", $script) . "\n}})(console);";
}

private static function getConsoleMethodForLevel($level)
{
return [
Logger::DEBUG => 'debug',
Logger::INFO => 'info',
Logger::NOTICE => 'info',
Logger::WARNING => 'warn',
Logger::ERROR => 'error',
Logger::CRITICAL => 'error',
Logger::ALERT => 'error',
Logger::EMERGENCY => 'error',
][$level] ?? 'log';
}

/**
* @return string[]
*/
Expand Down

0 comments on commit 620dca1

Please sign in to comment.