Installation
composer require cgarcia/audit-bundle
Add to AppKernel.php:
new DataDog\AuditBundle\DataDogAuditBundle(),
Database Setup Run migrations or schema update:
php app/console doctrine:migrations:diff
php app/console doctrine:migrations:migrate
Alternative:
php app/console doctrine:schema:update --force
First Use Case
User entity update via Symfony form)./audit (demo route) to verify captured changes.Automatic Logging
persist(), merge(), remove(), flush()) are audited without manual intervention.Product entity triggers an audit entry with:
Product), and diff of changed fields.Transaction Safety
Relation Tracking
Tag from a Post).PostTag updates).User Attribution
TokenStorage.SecurityBundle and a logged-in user.@AuditIgnore:
use DataDog\AuditBundle\Annotation\AuditIgnore;
/**
* @AuditIgnore
*/
class CreditCard {}
audit.log events:
# services.yml
services:
app.audit_listener:
class: AppBundle\EventListener\CustomAuditListener
tags:
- { name: kernel.event_listener, event: audit.log, method: onAuditLog }
/audit controller:
$auditRepo = $this->get('audit.repository');
$logs = $auditRepo->findBy(['entityClass' => 'AppBundle\Entity\Product']);
No DQL/SQL Tracking
EntityManager::createNativeQuery()) or DQL bypasses the bundle.$em->persist()) or wrap SQL in a service with manual audit logging.Performance Overhead
flush().$em->getConnection()->getConfiguration()->setSQLLogger(null);
$em->flush(); // No audit logs generated
User Attribution Gaps
null in audit logs.System):
// In a listener
$user = $tokenStorage->getToken()?->getUser() ?: new AnonymousUser();
Many-to-Many Edge Cases
doctrine.event_listeners.orm.default logging.config.yml:
doctrine:
dbal:
logging: true
profiling: true
audit.log events fire in dev.log:
grep "audit.log" app/logs/dev.log
public function onAuditLog(AuditLogEvent $event) {
$event->getLog()->setMetadata(['custom_field' => 'value']);
}
User updates):
public function onAuditLog(AuditLogEvent $event) {
if ($event->getLog()->getEntityClass() === User::class) {
$this->dispatchWebhook($event->getLog());
}
}
# config.yml
data_dog_audit:
entities:
include: ['AppBundle\Entity\Product']
exclude: ['AppBundle\Entity\CreditCard']
flush() + clear().How can I help you explore Laravel packages today?