Docs / Advanced / Extend the bundle

Extend the bundle

Change or drop an entry before it is written, or write your own source, output, reader, formatter, context provider or transformer.

Change or drop an entry

PreAuditLogEvent is dispatched before each write. A listener can change the entry or drop it:

src/EventListener/AuditListener.php
use SymfonyCorp\AuditLog\Event\PreAuditLogEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

#[AsEventListener]
final class AuditListener
{
    public function __invoke(PreAuditLogEvent $event): void
    {
        $entry = $event->getEntry();

        if ('messenger' === $entry->source && 'MESSAGE_SENT' === $entry->action) {
            $event->stopPropagation();   // drop the entry, keep MESSAGE_HANDLED and MESSAGE_FAILED
            return;
        }

        $event->setEntry($entry->withContext(['environment' => 'production']));
    }
}

Your own classes

Implement an interface and the bundle finds your class through autoconfiguration. No tag to write.

InterfaceMaker command
SourceInterfacemake:audit-source
OutputInterfacemake:audit-output
ReaderInterfacemake:audit-reader
FormatterInterfacemake:audit-formatter
ContextProviderInterfacemake:audit-context-provider
ValueTransformerInterfacemake:audit-transformer

The maker commands need symfony/maker-bundle, enabled in the current environment. A generated method throws a \LogicException until you write it.

terminal
$ bin/console make:audit-output Slack --purgeable --setupable

Custom source

A source is a class that implements SourceInterface and calls AuditLogger::log():

src/Audit/SchedulerSource.php
use SymfonyCorp\AuditLog\AuditLogger;
use SymfonyCorp\AuditLog\Entry\AuditEntry;
use SymfonyCorp\AuditLog\Source\SourceInterface;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\Scheduler\Event\PostRunEvent;

final class SchedulerSource implements SourceInterface
{
    public function __construct(private readonly AuditLogger $auditLogger)
    {
    }

    #[AsEventListener]
    public function onPostRun(PostRunEvent $event): void
    {
        $this->auditLogger->log(new AuditEntry(
            source: 'scheduler',
            action: 'EXECUTE',
            message: 'Schedule {schedule} ran',
            context: ['schedule' => $event->getMessageContext()->name],
        ));
    }
}

To record an entry from your own code without a source class, see Manual logging.

Custom output

src/Audit/SlackOutput.php
use SymfonyCorp\AuditLog\Attribute\AuditName;
use SymfonyCorp\AuditLog\Entry\AuditEntry;
use SymfonyCorp\AuditLog\Output\OutputInterface;

#[AuditName('slack')]
final class SlackOutput implements OutputInterface
{
    public function write(AuditEntry $entry): void
    {
        $this->slack->send(sprintf('[%s] %s', $entry->action, $entry->message));
    }
}

Custom reader

A reader implements getEntries(), count() and find(). Give it the name of the output it reads with #[AuditName]. Apply AuditEntryCriteria::$context, or throw when it is not empty.

Custom value transformer

Give the transformer a name with #[AuditName('mask_email')], then use it on a property with #[AuditTransform('mask_email')]. Without #[AuditName], use the service id (the class name when autowired). An unknown name fails cache:warmup. Without a cache warmup, for example in development, the field is recorded as *** and a warning is logged. See Transformations.