composer require ayaou/command-logger-bundle
config/bundles.php:
Ayaou\CommandLoggerBundle\AyaouCommandLoggerBundle::class => ['all' => true],
config/packages/command_logger.yaml:
command_logger:
enabled: true
command_log table:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Annotate a command to log its execution:
use Ayaou\CommandLoggerBundle\Attribute\CommandLogger;
use Symfony\Component\Console\Attribute\AsCommand;
#[AsCommand(name: 'app:test-command')]
#[CommandLogger]
class TestCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('Test command executed!');
return Command::SUCCESS;
}
}
Run the command:
php bin/console app:test-command
Verify logs:
php bin/console command-logger:show
Use the commands array in command_logger.yaml to log specific commands without annotations:
command_logger:
commands:
- app:cron-job:*
- make:migration
- make:model
*) support partial command names (e.g., app:cron-job:* logs all app:cron-job-* commands).doctrine:migrations:migrate).Disable selective logging and enable global logging:
command_logger:
enabled: true
commands: [] # Empty array logs all commands
For commands without annotations, ensure they’re listed in the commands config. For new commands, prefer the #[CommandLogger] attribute:
#[AsCommand(name: 'app:deploy')]
#[CommandLogger]
class DeployCommand extends Command { ... }
Use the CommandLoggerRepository service to fetch logs in custom logic:
use Ayaou\CommandLoggerBundle\Repository\CommandLoggerRepository;
class MyService {
public function __construct(
private CommandLoggerRepository $loggerRepo
) {}
public function getFailedDeployments(): array {
return $this->loggerRepo->findBy([
'commandName' => 'app:deploy',
'exitCode' => Command::FAILURE
]);
}
}
command-logger:purge periodically (e.g., weekly):
0 0 * * 0 php bin/console command-logger:purge --threshold=30
php bin/console command-logger:purge --threshold=90
php bin/console command-logger:show --error
php bin/console command-logger:show --id=123
#[CommandLogger] override config-based logging.commands config to avoid duplicates.*) match suffixes only (e.g., app:cron-job:* matches app:cron-job:daily but not app:cron-job-backup).commands:
- app:cron-job:*
- app:cron-*
composer.json if using Symfony 8:
composer require doctrine/orm:^3.0
make:test).purge_threshold to reduce database bloat.enabled: true in config.app:deploy vs. APP:DEPLOY).CommandLog entity to add metadata (e.g., userId, environment):
// src/Entity/CommandLog.php
#[ORM\Entity]
class CommandLog extends BaseCommandLog {
#[ORM\Column(type: 'string', nullable: true)]
private ?string $environment = null;
}
CommandLogger service to inject custom logic:
# config/services.yaml
Ayaou\CommandLoggerBundle\Service\CommandLogger:
arguments:
$environment: '%kernel.environment%'
$output->writeln(json_encode($input->getArgument('data')));
--threshold flag in command-logger:purge ignores the config value.#[AsCommand] instead of setName(). The bundle supports this natively (v1.6.0+).setName()) still work but may require updates for full compatibility.How can I help you explore Laravel packages today?