catchamonkey/console-logger-bundle
Installation:
composer require catchamonkey/console-logger-bundle
Ensure your composer.json includes the package under require.
Register the Bundle:
Add the bundle to app/AppKernel.php (or config/bundles.php for Symfony 4+):
new Catchamonkey\Bundle\ConsoleLoggerBundle\CatchamonkeyConsoleLoggerBundle(),
First Use Case:
Run a console command that throws an exception (e.g., php bin/console your:command). The exception will now be automatically logged via Monolog at the ERROR level.
app/config/config.yml for Monolog settings (if any customization is needed).app/logs/dev.log (or your configured log file) to confirm exceptions are captured.Automatic Exception Logging:
Integration with Monolog:
monolog.handler in config.yml).monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: error # Ensure this is set to capture ERROR-level logs
Workflow for Debugging:
dev.log to inspect exceptions in detail.monolog.handler.rotation).Custom Command Logging:
Symfony\Component\Console\Command\Command (default behavior).Pair with Sentry/Error Tracking: Use the logged exceptions as a trigger for external error-tracking services (e.g., Sentry’s Monolog handler). Example:
monolog:
handlers:
sentry:
type: service
id: monolog.handler.sentry
Log Context:
Enhance logs with command-specific context by overriding the bundle’s logger or using Monolog’s context feature:
// In your command:
$this->getApplication()->getLogger()->info('Custom context', ['command' => $this->getName()]);
Symfony 4+ Adaptation:
For Symfony 4/5, register the bundle in config/bundles.php:
return [
// ...
Catchamonkey\Bundle\ConsoleLoggerBundle\CatchamonkeyConsoleLoggerBundle::class => ['all' => true],
];
Bundle Not Triggering:
Command class or if the command is executed outside the Symfony kernel (e.g., via a custom script).php bin/console and extend Symfony\Component\Console\Command\Command.Log Level Mismatch:
level is set to debug or info, exceptions (logged as ERROR) may not appear.ERROR level or higher:
monolog:
handlers:
main:
level: error
Outdated Symfony Support:
symfony/console, symfony/dependency-injection).Missing Log Files:
app/logs/ is writable and Monolog is properly configured:
chmod -R 775 app/logs/
Check Event Listener:
The bundle registers an event subscriber (CatchamonkeyConsoleLoggerBundle\EventListener\ConsoleLoggerListener). Verify it’s loaded by dumping the container’s service tags:
php bin/console debug:container | grep console.logger
Override Logging Behavior: Extend the bundle’s logger by creating a custom subscriber:
use Symfony\Component\Console\Event\ConsoleErrorEvent;
class CustomConsoleLoggerSubscriber
{
public function onConsoleError(ConsoleErrorEvent $event)
{
$logger = $event->getCommand()->getApplication()->getLogger();
$logger->error('Custom error log', ['exception' => $event->getErrorObject()]);
}
}
Register it as a service with the kernel.event_listener tag.
Custom Log Format: Override the bundle’s logger by injecting a custom Monolog handler or processor. Example:
services:
app.custom_console_logger:
class: Monolog\Handler\StreamHandler
arguments: ['%kernel.logs_dir%/console_errors.log']
tags:
- { name: monolog.logger, channel: console }
Exclude Specific Commands: Filter exceptions by command name in a custom subscriber:
if ($event->getCommand()->getName() !== 'your:excluded-command') {
$logger->error($event->getErrorObject());
}
Add Metadata: Attach command metadata (e.g., input arguments) to logs:
$logger->error('Command failed', [
'command' => $event->getCommand()->getName(),
'input' => $event->getInput(),
'output' => $event->getOutput(),
]);
No Explicit Configuration: The bundle requires zero additional configuration beyond installation. All logging uses the default Monolog setup.
Symfony Flex Compatibility:
If using Symfony Flex, ensure auto-wiring is enabled in config/services.yaml for custom subscribers:
parameters:
container.autowiring.strict_mode: true
How can I help you explore Laravel packages today?