Docs / Advanced / Manual logging

Manual logging

Inject AuditLogger to record your own entries.

Record an entry

src/Service/OrderService.php
use SymfonyCorp\AuditLog\AuditLogger;
use SymfonyCorp\AuditLog\Entry\AuditEntry;

$this->auditLogger->log(new AuditEntry(
    source: 'order',
    action: 'CANCEL',
    message: 'Order #{order_id} cancelled',
    context: ['order_id' => $order->getId()],
));

AuditEntry takes these arguments:

AuditLogger adds the values of the context providers and calls your PreAuditLogEvent listeners, as for the entries of a source. See Extend the bundle.

Message placeholders

The message is a template with {placeholders}. Put the values in context, not in the message. Then you can group entries by message, and translate the message later.

The bundle replaces the placeholders at display time. In Twig, use the audit_log_message filter:

templates/order/history.html.twig
{{ entry|audit_log_message }}

In PHP, inject SymfonyCorp\AuditLog\Message\MessageRenderer and call render($entry):

src/Service/OrderHistory.php
$message = $this->messageRenderer->render($entry);   // Order #42 cancelled

Your own source

To record the entries of a part of your application in one place, write a source. See Extend the bundle.