Repository navigation
Attributes Reference
- Navigation
- Table of Contents
- Auditable
- IgnoreAudit
- AuditSensitive
- AuditMetadata
- AuditContext
- Best Practices
- Examples
This document provides comprehensive documentation for all attributes available in the Symfony Audit Bundle.
The #[Auditable] attribute is the primary attribute that enables audit tracking for an entity.
#[Auditable(
operations?: array,
ignoredFields?: array,
async?: bool
)]| Parameter | Type | Default | Description |
|---|---|---|---|
operations |
array |
['create', 'update', 'delete'] |
Operations to track |
ignoredFields |
array |
[] |
Fields to exclude from auditing |
async |
bool |
false |
Process audit logs asynchronously |
use BenMacha\AuditBundle\Attribute\Auditable;
#[Auditable]
class User
{
// All operations (create, update, delete) will be tracked
// All fields will be audited
}#[Auditable(operations: ['create', 'update'])]
class Product
{
// Only create and update operations will be tracked
// Delete operations will be ignored
}#[Auditable(ignoredFields: ['password', 'lastLogin', 'updatedAt'])]
class User
{
private string $username; // Will be audited
private string $email; // Will be audited
private string $password; // Will NOT be audited
private ?\DateTime $lastLogin; // Will NOT be audited
private \DateTime $updatedAt; // Will NOT be audited
}#[Auditable(async: true)]
class LargeEntity
{
// Audit logs will be processed asynchronously
// Useful for high-traffic entities to improve performance
}#[Auditable(
operations: ['create', 'update'],
ignoredFields: ['internalId', 'cache'],
async: true
)]
class OptimizedEntity
{
// Only create/update tracked
// Specific fields ignored
// Processed asynchronously
}- Always required for entities you want to audit
- Use
operationsto limit tracking to specific operations - Use
ignoredFieldsfor sensitive or irrelevant fields - Use
async: truefor high-traffic entities
The #[IgnoreAudit] attribute excludes specific fields or operations from audit tracking.
#[IgnoreAudit(operations?: array)]| Parameter | Type | Default | Description |
|---|---|---|---|
operations |
array |
[] |
Specific operations to ignore (if empty, ignores all) |
class User
{
private string $username; // Will be audited
#[IgnoreAudit]
private string $password; // Will NEVER be audited
#[IgnoreAudit]
private string $tempToken; // Will NEVER be audited
}class Article
{
private string $title; // Always audited
#[IgnoreAudit(operations: ['update'])]
private \DateTime $createdAt; // Audited on create/delete, not update
#[IgnoreAudit(operations: ['create', 'update'])]
private int $viewCount; // Only audited on delete
}class Statistics
{
#[IgnoreAudit(operations: ['create', 'update'])]
private int $hitCount; // Only track when deleted
#[IgnoreAudit(operations: ['delete'])]
private \DateTime $lastAccess; // Don't track on deletion
}- Sensitive data that should never be logged
- Frequently changing fields that would create noise
- Calculated fields that don't need tracking
- Temporary fields used for processing
The #[AuditSensitive] attribute marks fields as sensitive, enabling special handling like encryption or masking.
#[AuditSensitive(
encrypt?: bool,
mask?: bool,
hashAlgorithm?: string
)]| Parameter | Type | Default | Description |
|---|---|---|---|
encrypt |
bool |
false |
Encrypt the field value in audit logs |
mask |
bool |
false |
Mask the value in UI (show as ***) |
hashAlgorithm |
string |
'sha256' |
Algorithm for hashing sensitive data |
class User
{
#[AuditSensitive(mask: true)]
private string $socialSecurityNumber; // Shows as *** in UI
#[AuditSensitive(mask: true)]
private string $creditCardNumber; // Shows as *** in UI
}class Patient
{
#[AuditSensitive(encrypt: true)]
private string $medicalRecord; // Encrypted in database
#[AuditSensitive(encrypt: true, hashAlgorithm: 'sha512')]
private string $geneticData; // Encrypted with SHA-512
}class Employee
{
#[AuditSensitive(encrypt: true, mask: true)]
private string $salary; // Encrypted AND masked
#[AuditSensitive(encrypt: true, mask: true, hashAlgorithm: 'sha256')]
private string $personalNotes; // Full protection
}- Encryption keys must be properly managed
- Masked fields are still stored in plaintext (use with encrypt)
- Hash algorithms should be cryptographically secure
- Performance impact of encryption should be considered
- PII (Personally Identifiable Information)
- Financial data
- Medical records
- Legal documents
- Any sensitive business data
The #[AuditMetadata] attribute adds custom metadata to audit logs for enhanced categorization and searching.
#[AuditMetadata(
tags?: array,
indexed?: bool,
ttl?: int,
customData?: array
)]| Parameter | Type | Default | Description |
|---|---|---|---|
tags |
array |
[] |
Custom tags for categorization |
indexed |
bool |
false |
Whether to index this field for searching |
ttl |
int |
null |
Time-to-live in seconds |
customData |
array |
[] |
Additional custom metadata |
class User
{
#[AuditMetadata(tags: ['pii', 'gdpr'])]
private string $email; // Tagged for GDPR compliance
#[AuditMetadata(tags: ['sensitive', 'hr'])]
private string $department; // Tagged for HR category
}class Product
{
#[AuditMetadata(indexed: true)]
private string $sku; // Indexed for fast searching
#[AuditMetadata(indexed: true, tags: ['inventory'])]
private int $quantity; // Indexed and tagged
}class Session
{
#[AuditMetadata(ttl: 86400)] // 24 hours
private string $sessionId;
#[AuditMetadata(ttl: 2592000)] // 30 days
private \DateTime $lastActivity;
}class Order
{
#[AuditMetadata(
tags: ['financial', 'order'],
customData: [
'department' => 'sales',
'region' => 'north_america',
'compliance_level' => 'high'
]
)]
private float $totalAmount;
}class Document
{
#[AuditMetadata(
tags: ['legal', 'contract', 'confidential'],
indexed: true,
ttl: 31536000, // 1 year
customData: [
'classification' => 'confidential',
'retention_policy' => 'legal_hold',
'access_level' => 'restricted'
]
)]
private string $contractText;
}- Compliance requirements (GDPR, HIPAA, etc.)
- Data classification
- Search optimization
- Retention policies
- Business categorization
The #[AuditContext] attribute provides additional context for audit operations at the entity level.
#[AuditContext(
reason?: string,
category?: string,
priority?: string,
metadata?: array
)]| Parameter | Type | Default | Description |
|---|---|---|---|
reason |
string |
null |
Human-readable reason for changes |
category |
string |
null |
Category for grouping related changes |
priority |
string |
'medium' |
Priority level (low, medium, high, critical) |
metadata |
array |
[] |
Additional context metadata |
#[AuditContext(
reason: 'User profile management',
category: 'user_operations'
)]
class User
{
// All changes will include this context
}#[AuditContext(
reason: 'Financial transaction processing',
category: 'financial',
priority: 'critical'
)]
class Transaction
{
// High-priority audit logging
}#[AuditContext(
reason: 'Patient data management',
category: 'healthcare',
priority: 'high',
metadata: [
'compliance' => 'HIPAA',
'data_classification' => 'PHI',
'retention_years' => 7,
'access_restrictions' => 'medical_staff_only'
]
)]
class Patient
{
// Comprehensive context for healthcare compliance
}#[AuditContext(
reason: 'Inventory management operations',
category: 'supply_chain',
priority: 'medium',
metadata: [
'business_unit' => 'logistics',
'process_type' => 'automated',
'sla_requirement' => '99.9%'
]
)]
class Inventory
{
// Business process context
}- Compliance documentation
- Business process tracking
- Priority-based monitoring
- Contextual reporting
- Audit trail documentation
#[Auditable(operations: ['create', 'update'])]
#[AuditContext(
reason: 'Customer data management',
category: 'crm',
priority: 'high'
)]
class Customer
{
#[AuditMetadata(tags: ['pii'], indexed: true)]
private string $email;
#[AuditSensitive(encrypt: true, mask: true)]
#[AuditMetadata(tags: ['sensitive', 'financial'])]
private string $creditScore;
#[IgnoreAudit]
private string $temporaryToken;
}// For high-traffic entities
#[Auditable(
async: true,
ignoredFields: ['lastAccess', 'hitCount', 'cache']
)]
class HighTrafficEntity
{
// Only audit important fields asynchronously
}#[Auditable]
#[AuditContext(
reason: 'GDPR compliance data processing',
category: 'gdpr',
priority: 'critical'
)]
class GDPREntity
{
#[AuditSensitive(encrypt: true)]
#[AuditMetadata(
tags: ['pii', 'gdpr'],
ttl: 2592000, // 30 days
customData: ['gdpr_category' => 'personal_data']
)]
private string $personalData;
}class SecuritySensitiveEntity
{
#[AuditSensitive(encrypt: true, mask: true)]
#[IgnoreAudit(operations: ['update'])] // Only log creation
private string $apiKey;
#[AuditSensitive(mask: true)]
#[AuditMetadata(tags: ['auth', 'security'])]
private string $passwordHash;
}#[Auditable(operations: ['create', 'update'])]
#[AuditContext(
reason: 'Product catalog management',
category: 'ecommerce',
priority: 'medium'
)]
class Product
{
#[AuditMetadata(indexed: true, tags: ['catalog'])]
private string $sku;
#[AuditMetadata(tags: ['pricing'])]
private float $price;
#[AuditSensitive(mask: true)]
#[AuditMetadata(tags: ['financial', 'cost'])]
private float $cost;
#[IgnoreAudit]
private int $viewCount;
#[IgnoreAudit(operations: ['update'])]
private \DateTime $createdAt;
}#[Auditable]
#[AuditContext(
reason: 'Patient health record management',
category: 'healthcare',
priority: 'critical',
metadata: ['compliance' => 'HIPAA']
)]
class Patient
{
#[AuditMetadata(indexed: true, tags: ['identifier'])]
private string $patientId;
#[AuditSensitive(encrypt: true)]
#[AuditMetadata(
tags: ['phi', 'medical'],
ttl: 220752000, // 7 years
customData: ['hipaa_category' => 'protected_health_info']
)]
private string $medicalHistory;
#[AuditSensitive(encrypt: true, mask: true)]
#[AuditMetadata(tags: ['pii', 'sensitive'])]
private string $socialSecurityNumber;
#[IgnoreAudit]
private \DateTime $lastLoginTime;
}#[Auditable(async: true)] // High volume, process async
#[AuditContext(
reason: 'Financial transaction processing',
category: 'finance',
priority: 'critical',
metadata: [
'compliance' => 'SOX',
'audit_requirement' => 'mandatory'
]
)]
class Transaction
{
#[AuditMetadata(indexed: true, tags: ['transaction', 'reference'])]
private string $transactionId;
#[AuditSensitive(encrypt: true)]
#[AuditMetadata(
tags: ['financial', 'amount'],
customData: ['currency_sensitive' => true]
)]
private float $amount;
#[AuditMetadata(indexed: true, tags: ['account'])]
private string $accountNumber;
#[IgnoreAudit(operations: ['update'])] // Immutable after creation
private \DateTime $processedAt;
}These examples demonstrate how to combine multiple attributes effectively for different business domains while maintaining security, compliance, and performance considerations.
Symfony Audit Bundle | GitHub Repository | Issues | Discussions
Last updated: 2025-08-27 14:00:41 UTC