PHP and Twig
Read the entries back in PHP with ReaderInterface, then show them in a template with the audit_log_message filter.
Readable outputs
The dbal, elasticsearch, mongodb and in_memory outputs can read their entries back. If you have more than one of them, select one with --output=<name> in the commands and ?output=<name> in the EasyAdmin URL.
The commands and the EasyAdmin viewer record each read. The PHP reader does not record a read.
PHP
Inject ReaderInterface:
use SymfonyCorp\AuditLog\Reader\AuditEntryCriteria;
use SymfonyCorp\AuditLog\Reader\ReaderInterface;
$criteria = new AuditEntryCriteria(
offset: 0,
limit: 50,
source: 'doctrine_orm',
action: 'INSERT',
from: new \DateTimeImmutable('-30 days'),
);
$total = $this->reader->count($criteria);
$entries = $this->reader->getEntries($criteria);The ReaderInterface alias exists only when you have one readable output. With more than one, pick a reader by its service id. To list the ids, run bin/console debug:container --tag=symfonycorp_audit_log.reader:
public function __construct(
#[Autowire(service: 'symfonycorp_audit_log.reader.dbal')]
private readonly ReaderInterface $reader,
) {
}Filter by context
context finds the entries of one person or one object:
new AuditEntryCriteria(context: ['user_identifier' => 'alice@example.com']);
new AuditEntryCriteria(context: ['subject_class' => Book::class, 'subject_id' => 42]);user_identifier and ip need the user and request context providers. The Doctrine sources write subject_class and subject_id. To use the filter for a GDPR access request, read Access requests first.
The reader compares each value as a string: 42 and '42' match. It filters only on the keys of indexed_context, and throws on another key. The dbal, elasticsearch and mongodb outputs index these keys:
symfonycorp_audit_log:
# tenant_id is a custom key. Default: the first four
indexed_context: [user_identifier, subject_class, subject_id, ip, tenant_id]A value that is not a string or an integer, or that is longer than 255 characters, is not indexed. The reader throws if a filter value is longer than 255 characters.
When you change the list, update the storage of each output before you deploy:
dbal: generate a Doctrine migration. Without the new columns, the writes fail.elasticsearch: create a new index.mongodb: runaudit-log:setup. It creates the missing indexes.
With dbal and elasticsearch, the filter does not find the entries written before the change.
Twig
Twig does not read the storage. Read the entries in PHP with ReaderInterface, then pass them to the template:
namespace App\Controller;
use SymfonyCorp\AuditLog\Reader\AuditEntryCriteria;
use SymfonyCorp\AuditLog\Reader\ReaderInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
final class AuditController extends AbstractController
{
#[Route('/audit', name: 'app_audit')]
#[IsGranted('ROLE_AUDIT_LOG_VIEWER')]
public function __invoke(ReaderInterface $reader): Response
{
$criteria = new AuditEntryCriteria(source: 'doctrine_orm', limit: 10);
return $this->render('audit/index.html.twig', [
'entries' => $reader->getEntries($criteria),
]);
}
}With twig/twig installed, the audit_log_message filter replaces the placeholders of the message:
{% for entry in entries %}
{{ entry.occurredAt|date('Y-m-d H:i') }} {{ entry|audit_log_message }}
{% endfor %}Twig escapes the result. Do not add |raw: the context values can come from users.
Warning - the entries can hold personal data. Restrict access to the pages that show them, as for the EasyAdmin viewer.