bizkit/loggable-command-bundle
composer require bizkit/loggable-command-bundle
config/bundles.php:
return [
// ...
Bizkit\LoggableCommandBundle\BizkitLoggableCommandBundle::class => ['all' => true],
];
config/packages/monolog.yaml:
monolog:
handlers:
loggable_commands:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ["loggable_commands"]
use Bizkit\LoggableCommandBundle\Attribute\Loggable;
#[Loggable]
class MyCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->logger->info('Command executed');
return Command::SUCCESS;
}
}
php bin/console my:command
Logs will now appear in a dedicated file (e.g., var/log/dev.loggable_commands).@Loggable on a command handling sensitive operations (e.g., payments, API calls).var/log/{env}.loggable_commands, reducing noise in the main log.tail -f var/log/dev.loggable_commands.#[Loggable(level: 'debug', handler: 'custom_rotating')]
class PaymentCommand extends Command { ... }
/**
* @Loggable(level="debug", handler="custom_rotating")
*/
class LegacyCommand extends Command { ... }
stream or rotating_file via config:
bizkit_loggable_command:
default_handler: rotating_file
handlers:
rotating_file:
type: rotating_file
path: "%kernel.logs_dir%/commands/%kernel.environment%.log"
max_files: 7
LoggableHandlerFactory:
class CustomHandlerFactory extends LoggableHandlerFactory
{
public function createHandler(array $config): HandlerInterface
{
return new FingerprintHandler(...);
}
}
Register in config/packages/bizkit_loggable_command.yaml:
bizkit_loggable_command:
handler_factories:
fingerprint: App\Handler\CustomHandlerFactory
stdout/stderr for specific levels:
#[Loggable(level: 'error', stream: 'stderr')]
class CriticalCommand extends Command { ... }
bizkit_loggable_command:
default_stream: stdout
loggable_commands from other handlers:
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
channels: ["!loggable_commands"] # Exclude
#[Loggable]
class ProcessPaymentHandler implements MessageHandlerInterface
{
public function __invoke(ProcessPaymentMessage $message)
{
$this->logger->info('Processing payment', ['amount' => $message->amount]);
}
}
config/packages/messenger.yaml:
messenger:
transports:
async: { dsn: 'sync://' }
routing:
'App\Message\ProcessPaymentMessage': async
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->logger->info('Command started', ['args' => $input->getArguments()]);
try {
// ...
} finally {
$this->logger->info('Command finished');
}
return Command::SUCCESS;
}
$this->logger->debug('User data', [
'user_id' => $user->id,
'action' => 'update_profile',
]);
Channel Mismatch:
monolog.yaml includes the loggable_commands channel:
channels: ["loggable_commands"]
var/log/dev.log for errors.Handler Factory Not Found:
ClassNotFoundException. Fix: Clear cache (php bin/console cache:clear).PHP 7.2+ Attribute Parsing:
@Loggable ignored. Fix: Downgrade to annotations or upgrade PHP.Log Rotation Conflicts:
max_files and file_permission in config.Messenger Async Delays:
sync transport for debugging or add timestamps:
$this->logger->info('Async task', ['timestamp' => time()]);
bizkit_loggable_command:
debug: true # Logs handler creation to main log
var/log/dev.log for lines like:
[BizkitLoggableCommandBundle] Created handler 'rotating_file' for command 'app:my_command'
default_stream to stdout for quick checks:
bizkit_loggable_command:
default_stream: stdout
Custom Attributes:
LoggableAttribute to add metadata:
#[Attribute]
class PriorityLoggable extends LoggableAttribute
{
public function __construct(public int $priority = 1) {}
}
LoggableCommandSubscriber to handle the new attribute.Handler Context:
$handler = $this->get('loggable_command.handler_factory')->createHandler([
'context' => ['user_id' => $user->id],
]);
Log Level Overrides:
$this->logger->setLevel(LogLevel::DEBUG); // Override for a command
Integration with ELK/Stackdriver:
fingerprint handler to add structured metadata:
bizkit_loggable_command:
handlers:
elk:
type: fingerprint
path: "%kernel.logs_dir%/elk.log"
fingerprint: ["@command_name", "@user_id"]
level: 'error' by default and add debug only where needed.php bin/console cache:clear bizkit_loggable_command
async handlers (Symfony 5.3+):
bizkit_loggable_command:
handlers:
async:
type: async
handler: stream
buffer_size: 50
How can I help you explore Laravel packages today?