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:
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.
| Interface | Maker command |
|---|---|
SourceInterface | make:audit-source |
OutputInterface | make:audit-output |
ReaderInterface | make:audit-reader |
FormatterInterface | make:audit-formatter |
ContextProviderInterface | make:audit-context-provider |
ValueTransformerInterface | make:audit-transformer |
The maker commands need symfony/maker-bundle, enabled in the current environment. A generated method throws a \LogicException until you write it.
$ bin/console make:audit-output Slack --purgeable --setupableCustom source
A source is a class that implements SourceInterface and calls AuditLogger::log():
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
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));
}
}write()does not catch its exceptions. The bundle logs them, or throws them whensilent_failuresisfalse.- To support
audit-log:purge, also implementPurgeableInterface. To supportaudit-log:setup, implementSetupableInterface.setup()must throw on failure. #[AuditName]gives the short name for--output=<name>and?output=<name>. Without it, the name is the service id. Give a reader the same name as its output.- Two outputs with the same name fail the container compilation.
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.