Roll Your Own Audit Logging

Curated ago by @jeremysmithco

Description

Here’s a simple audit logging solution using an AuditLog ActiveRecord model with a polymorphic association, a Sidekiq worker, and an audit_log controller method. I wrote about the approach in Audit Logging in Rails.

1
2
3
4
5
class ApplicationController < ActionController::Base
  def audit_log(auditable, account, event)
    AuditLogWorker.perform_async(auditable.to_global_id, account.id, current_user.id, event)
  end
end

The audit_log method can be called from any controller action to log events for any auditable model.