Audit Logging Integration Guide

Current State

The audit service is currently passive - it provides infrastructure but doesn’t automatically capture changes. You need to manually integrate it into your business logic.

Integration Approaches

Approach 1: Manual Audit Logging (Current - Simple)

Manually call CreateAuditLog in your service methods. Example: Adding audit to ApprovalService
Pros:
  • ✅ Simple and explicit
  • ✅ Full control over what’s logged
  • ✅ Easy to understand
Cons:
  • ❌ Manual work for each operation
  • ❌ Easy to forget
  • ❌ Repetitive code

Wrap services with audit decorators that automatically log operations.
Pros:
  • ✅ Automatic audit logging
  • ✅ Separation of concerns
  • ✅ Easy to add/remove
Cons:
  • ❌ More complex setup
  • ❌ Requires interface-based design

Approach 3: Event-Driven (Best for Microservices)

Use domain events to trigger audit logging.
Pros:
  • ✅ Complete decoupling
  • ✅ Async processing possible
  • ✅ Easy to add multiple listeners
  • ✅ Microservice-ready
Cons:
  • ❌ Most complex
  • ❌ Eventual consistency
  • ❌ Requires event bus infrastructure

Approach 4: Middleware/Interceptor (HTTP Level)

Capture audit logs at the HTTP handler level.
Pros:
  • ✅ Centralized
  • ✅ Captures all HTTP operations
  • ✅ Easy to enable/disable
Cons:
  • ❌ No access to domain-level before/after states
  • ❌ HTTP-only (doesn’t capture background jobs)
  • ❌ Less granular

Phase 1: Start with Manual (Quick Win)

Add audit logging to critical operations:
  • Leave request approval/rejection
  • Leave type creation/deletion
  • Policy changes

Phase 2: Add Event-Driven (Scale)

Refactor to use domain events for automatic audit logging.

Phase 3: Add Middleware (Coverage)

Add HTTP middleware for comprehensive coverage of all API calls.

Example: Complete Integration

Here’s how to integrate audit logging into the approval service:

What Gets Audited?

Based on the AuditAction enum in audit_log.go:
  • CREATE - New leave requests, leave types
  • UPDATE - Policy changes, leave type modifications
  • DELETE - Leave type deletion
  • APPROVE - Leave request approvals
  • REJECT - Leave request rejections
  • CANCEL - Leave request cancellations
  • EXPORT - Report exports
  • VIEW - Sensitive data access

Next Steps

  • Add audit middleware for HTTP-level logging