bramus/monolog-colored-line-formatter
Adds a colored line formatter for Monolog, making console log output easier to scan with ANSI colors. Works as a drop-in formatter to highlight log levels, timestamps, and messages for CLI tools, workers, and development environments.
Log::stack(), config/logging.php, and environment-based logging configurations.composer require bramus/monolog-colored-line-formatter) with no build steps or dependencies.setColors() or subclassing for team-specific themes.single, daily, syslog) via handler configuration.grep, awk, or regex may fail if ANSI sequences are not stripped. Example:
grep --color=never error.log # Safely ignores ANSI
local/development environments, or also in staging (where ANSI may still be useful)?stream to php://stdout)?daily, single) from coloring in production.php_sapi_name() !== 'cli').$processor = new class implements \Monolog\Processor\ProcessorInterface {
public function __invoke(array $record) {
$record['formatted'] = preg_replace('/\x1B\[[0-9;]*m/', '', $record['formatted']);
return $record;
}
};
$formatter = new \Bramus\Monolog\Formatter\ColoredLineFormatter(
"[%datetime%] %channel%.%level_name%: %message%\n",
"Y-m-d H:i:s",
true
);
php artisan [command] output.single, daily handlers) will not display colors in terminals.config/logging.php (default channels).AppServiceProvider (custom log stack extensions).stream to php://stdout) vs. file-based handlers (e.g., daily).// config/logging.php
'channels' => [
'colored' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'formatter' => \Bramus\Monolog\Formatter\ColoredLineFormatter::class,
],
],
// app/Providers/AppServiceProvider.php
public function boot()
{
if (app()->environment('local')) {
$this->app['log']->stack(function ($stack) {
$stack->push(
(new \Monolog\Handler\StreamHandler(storage_path('logs/laravel.log')))
->setFormatter(new \Bramus\Monolog\Formatter\ColoredLineFormatter())
);
});
}
}
// In an Artisan command
public function handle()
{
Log::channel('colored')->info('Command started');
}
$processor = new class implements \Monolog\Processor\ProcessorInterface {
public function __invoke(array $record) {
if (!app()->runningInConsole()) {
$record['formatted'] = preg_replace('/\x1B\[[0-9;]*m/', '', $record['formatted']);
}
return $record;
}
};
$handler->pushProcessor($processor);
composer.json if using edge cases.php://stdout).local environment.How can I help you explore Laravel packages today?