Skip to content

Commit

Permalink
Update BrowserConsoleHandler console output method
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 Jul 26, 2022
1 parent 305444b commit 8cb8c62
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 @@ -15,6 +15,7 @@
use Monolog\Formatter\LineFormatter;
use Monolog\Utils;
use Monolog\LogRecord;
use Monolog\Logger;

use function count;
use function headers_list;
Expand Down Expand Up @@ -173,7 +174,7 @@ private static function generateScript(): string
$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(static::getConsoleMethodForLevel($record->level), self::handleStyles($record->formatted));
} else {
$script = array_merge(
$script,
Expand All @@ -188,6 +189,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 8cb8c62

Please sign in to comment.