Skip to content

Conversation

@matiasperrone-exo
Copy link

@matiasperrone-exo matiasperrone-exo commented Dec 24, 2025

Overview

This PR introduces a comprehensive Doctrine ORM audit logging framework with support for both database-backed and OpenTelemetry-based audit strategies.

Key Features

  • Audit Context: New AuditContext class that captures user, request, and application metadata
  • Event Listener: AuditEventListener that intercepts Doctrine ORM events (insertions, updates, deletions, collection changes)
  • Audit Strategies:
    • AuditLogStrategy - Database persistence strategy
    • AuditLogOtlpStrategy - OpenTelemetry/Elasticsearch integration
  • Formatter System: Extensible formatter architecture for different entity types
    • AbstractAuditLogFormatter - Base class for all formatters
    • Entity-specific formatters (Creation, Update, Deletion, Collection)
    • Child entity formatter factory for nested entity changes
  • Job Queue Integration: EmitAuditLogJob for asynchronous audit log emission
  • Configuration: Audit configuration file for entity-specific strategies

Files Added

  • app/Audit/AbstractAuditLogFormatter.php
  • app/Audit/AuditContext.php
  • app/Audit/AuditEventListener.php
  • app/Audit/AuditLogFormatterFactory.php
  • app/Audit/AuditLogOtlpStrategy.php
  • app/Audit/AuditLogStrategy.php
  • app/Audit/AuditLoggerFactory.php
  • app/Audit/AuditProvider.php
  • app/Audit/Interfaces/IAuditStrategy.php
  • app/Audit/Interfaces/IAuditLogFormatter.php
  • app/Audit/Interfaces/IAuditLogFormatterFactory.php
  • app/Audit/Interfaces/ILogger.php
  • app/Audit/ConcreteFormatters/EntityCreationAuditLogFormatter.php
  • app/Audit/ConcreteFormatters/EntityUpdateAuditLogFormatter.php
  • app/Audit/ConcreteFormatters/EntityDeletionAuditLogFormatter.php
  • app/Audit/ConcreteFormatters/EntityCollectionUpdateAuditLogFormatter.php
  • app/Audit/ConcreteFormatters/ChildEntityFormatters/IChildEntityAuditLogFormatter.php
  • app/Audit/ConcreteFormatters/ChildEntityFormatters/ChildEntityFormatterFactory.php
  • app/Jobs/EmitAuditLogJob.php
  • config/audit_log.php

Files Modified

  • app/ModelSerializers/SerializerRegistry.php - Code formatting improvements

Architecture Highlights

  • Strategy Pattern: Pluggable audit strategies (DB vs OTLP)
  • Factory Pattern: Dynamic formatter and logger creation
  • Event-Driven: Hooks into Doctrine's OnFlush event
  • Metadata Capture: Comprehensive context including user info, HTTP details, and route information
  • Error Handling: Graceful degradation with logging

Configuration

The audit system checks for opentelemetry.enabled configuration:

  • If enabled: Uses OpenTelemetry strategy for remote logging
  • If disabled: Falls back to database strategy

Notes

  • Testing environment bypasses audit logging
  • Configuration-driven per-entity audit strategies supported
  • Collection updates capture insert/delete diffs
  • Compatible with both Doctrine ClassMetadata variations

Task:

Ref: https://app.clickup.com/t/86b7xy5fp

@matiasperrone-exo matiasperrone-exo self-assigned this Dec 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants