damienharper/doctrine-audit-bundle
Installation
composer require damienharper/auditor-bundle
Add to config/bundles.php (Symfony 5+):
return [
// ...
DamienHarper\AuditorBundle\DamienHarperAuditorBundle::class => ['all' => true],
];
Configure in config/packages/auditor.yaml
damien_harper_auditor:
connection: default # Doctrine connection name
logger: true # Enable logging (optional)
log_entry_class: App\Entity\AuditLogEntry # Custom log entry class
First Use Case: Enable Auditing on an Entity
Annotate your entity with @Audited:
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @Gedmo\Loggable
*/
class Product {}
Run migrations to create the audit table (if using Doctrine migrations):
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Enable Auditing for Entities
Use @Gedmo\Loggable on any Doctrine entity to track all CRUD operations:
/**
* @Gedmo\Loggable
*/
class User {}
Customize Audit Logs
Override the default log entry class (App\Entity\AuditLogEntry) to add custom fields:
use Gedmo\Loggable\Entity\AuditEntry;
class CustomAuditLogEntry extends AuditEntry
{
/**
* @ORM\Column(type="string", nullable=true)
*/
private $customField;
}
Querying Audit Data Use Doctrine queries to fetch audit logs:
$auditLogs = $entityManager->getRepository(AuditLogEntry::class)
->findBy(['objectClass' => User::class]);
Exclude Fields from Auditing
Use @Gedmo\Loggable\Exclude to skip specific fields:
/**
* @Gedmo\Loggable\Exclude
*/
private $password;
Bulk Operations
For mass updates, use Auditor::setFlushMode(Auditor::FLUSH_DISABLED) to disable auditing temporarily:
$auditor = $entityManager->getExtension('gedmo_auditor');
$auditor->setFlushMode(Auditor::FLUSH_DISABLED);
// Bulk operations here...
$auditor->setFlushMode(Auditor::FLUSH_ENABLED);
Performance Overhead
FLUSH_DISABLED.doctrine:query profiling.Circular References
@Gedmo\Loggable\Exclude or lazy-loading (@ORM\LazyGroup).Migration Conflicts
doctrine:schema:update --force.Symfony Cache Issues
php bin/console cache:clear
Enable Logging
Set logger: true in auditor.yaml to debug audit events in var/log/dev.log.
Check Audit Events
Use the AuditorListener to log events manually:
$event = $event->getObject();
$this->logger->info('Audited entity: ' . get_class($event));
Custom Audit Strategies
Implement Gedmo\Loggable\LogEntry\AbstractLogEntry to create custom log entry classes.
Event Subscribers Extend auditing logic via Symfony events:
use Gedmo\Loggable\LoggableListener;
class CustomAuditorSubscriber implements EventSubscriber
{
public function getSubscribedEvents()
{
return [
LoggableListener::PRE_FLUSH_EVENT => 'onPreFlush',
];
}
}
Filtering Audit Logs Use DQL to filter logs by date, user, or entity:
$qb = $entityManager->createQueryBuilder();
$qb->select('e')
->from(AuditLogEntry::class, 'e')
->where('e.timestamp > :date')
->setParameter('date', new \DateTime('-7 days'));
Asynchronous Auditing Offload auditing to a queue (e.g., Symfony Messenger) for high-traffic apps:
# config/packages/messenger.yaml
framework:
messenger:
transports:
async_audit: '%env(MESSENGER_TRANSPORT_DSN)%'
How can I help you explore Laravel packages today?