DEV Community

Cover image for A Developer’s Guide to the Command Logger Bundle for Symfony
Mohamed
Mohamed

Posted on

A Developer’s Guide to the Command Logger Bundle for Symfony

For when you can’t access log files, but you can see the database.

Ever been in a situation where a command fails on production, but you don’t have access to the server’s log files? All you can do is connect to the database. For fast debugging, you don’t need to see every line of output; you just need to answer three simple questions:

  • Did my command run?
  • When did it run?
  • Did it complete successfully or did it fail?

This is the exact scenario the Command Logger Bundle was built for. It’s a purpose-built solution that provides a robust, database-backed audit trail for your console commands, giving you the critical visibility you need, right where you can see it.
What It Does

This bundle automatically captures metadata about your command executions and stores it in a dedicated command_log database table. For each execution, it records:

  • The command’s name
  • The arguments it was run with (in JSON format)
  • The start and end times
  • The final exit code
  • Any error messages
  • A unique executionToken for tracking

A Note on Scope: Fast Auditing, Not Output Logging

It’s important to note that this bundle is designed for fast debugging and auditing. Its primary goal is to track when a command has been executed and if its outcome was OK or not.

It does not capture the command’s output (e.g., messages written to the console or logs generated by Monolog within the command). It only logs the metadata about the execution itself.
Installation and Configuration

Getting started is a simple two-step process.

Step 1: Install with Composer

First, add the bundle to your project using Composer:

composer require ayaou/command-logger-bundle

If your project doesn’t use Symfony Flex, you’ll need to manually register the bundle in config/bundles.php:

// config/bundles.php
return [
    // ...
    Ayaou\CommandLoggerBundle\AyaouCommandLoggerBundle::class => ['all' => true],
];
Enter fullscreen mode Exit fullscreen mode

Step 2: Configure the Bundle [Optional]

Next, create a configuration file at config/packages/command_logger.yaml to control the bundle's behaviour.

# config/packages/command_logger.yaml
command_logger:
  # Enable or disable all logging globally. Defaults to true.
  enabled: true

  # Days after which old logs are automatically deleted.
  purge_threshold: 100

  # A list of commands to log, especially useful for third-party
  # bundles where you cannot add attributes. Wildcards are supported.
  commands:
    - app:example-command
    - app:another-command
    - make:*
Enter fullscreen mode Exit fullscreen mode

How to Log Your Commands

You have two ways to specify which commands should be logged.

1. The Attribute Method (Recommended)

For commands within your own project, the easiest method is to use the #[CommandLogger] attribute directly on the command class.

use Ayaou\CommandLoggerBundle\Attribute\CommandLogger;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
// ...

#[AsCommand(name: 'app:example-command')]
#[CommandLogger]
class ExampleCommand extends Command
{
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $output->writeln('Executing example command...');
        return Command::SUCCESS;
    }
}
Enter fullscreen mode Exit fullscreen mode

That’s it! The bundle will now automatically log every execution of app:example-command.

2. The Configuration Method

If you want to log a command that you cannot modify (for example, a command from a third-party bundle), you can add its name to the commands array in your command_logger.yaml file, as shown in the configuration step.

Viewing and Filtering Logs

Once you have logged some command executions, you can easily view them from your terminal using the built-in command-logger:show command. This is perfect for quickly checking the status of recent jobs.

  • View the latest 10 logs:

bin/console command-logger:show

Filter by command name:
Enter fullscreen mode Exit fullscreen mode

bin/console command-logger:show app:example-command

  • Quickly find failed commands (the primary use case!):

bin/console command-logger:show --error

Use — success to show commands that run successfully.

  • Find a specific log entry by its ID:

bin/console command-logger:show --id=123

The command also supports interactive pagination; simply press Enter to load more entries.

Keeping Your Logs Tidy

A log table can grow quickly. The Command Logger Bundle provides tools to prevent it from getting out of control.

Automatic Purging

The purge_threshold option in your configuration file automatically deletes logs older than the specified number of days. And of course, do not forgot to put this command in a cron job that runs periodically.

Manual Purging

If you need to clean up logs manually, you can run the command-logger:purge command.

  • Purge logs using the configured threshold
    bin/console command-logger:purge

  • Purge logs older than 30 days, overriding the config
    bin/console command-logger:purge --threshold=30

Conclusion

The Command Logger Bundle offers a focused and powerful solution for auditing your Symfony console commands, especially in environments where direct log file access is limited. By providing a persistent, queryable database log with built-in tools for viewing and maintenance, it dramatically improves your ability to debug and monitor your application’s command-line activity.

This bundle is distributed under the MIT Licence. For more information, please visit the official repository.

Top comments (0)