Auditron ๐
Know who changed what, when, and why โ audit logging built for Rails APIs.
Built-in retention, flexible actor tracking, and a clean query DSL, for API-first Rails apps.
The problem every API team hits
Your support team asks: "Who changed this user's email last Tuesday?"
You check the database. Nothing. You check the logs. Nothing useful. So you reach for an audit gem, and it assumes current_user lives in a controller, stores a full copy of the record on every change, and leaves you to write your own cleanup job.
That works for a classic session-based Rails app. It gets in your way when you're building with JWT, service objects, and background jobs.
Auditron was designed for how modern Rails APIs are actually built:
- ๐ JWT and service-layer friendly. Set the actor anywhere, not just in controllers.
- ๐ฆ Diff-only storage. Stores only what changed, not the whole object. Change one column on a 30-attribute model and you write one column.
- ๐งน Built-in retention. Configure once, run a job, and logs clean themselves up.
- ๐ Chainable query DSL. Find exactly what you need without writing raw SQL.
paper_trailwrites a full snapshot on every change, so storage grows fast at scale. Bothpaper_trailandauditedtie actor tracking to a controller'scurrent_user, which doesn't exist in JWT or service-layer code. Neither ships with log retention.
Who needs this?
- API-first Rails apps using JWT or token-based auth
- Apps under GDPR, HIPAA, or SOC2 requirements that need audit trails
- Teams tired of audit tables bloating their database
- Anyone who needs to answer "who changed this, when, and why?"
At a glance
class Account < ApplicationRecord
include Auditron::Auditable
auditable only: [:email, :role, :status]
end
# someone updates their profile...
account.update!(first_name: "Jane")
# now you know exactly what happened
account.audit_logs.last
# => #<AuditLog
# action: "updated"
# changed_fields: { "first_name" => ["John", "Jane"] }
# actor_type: "Account"
# actor_id: 1
# ip_address: "192.168.1.1"
# created_at: "2026-04-04T08:00:00Z"
# >Install
Add it to your Gemfile:
gem "auditron"Then run:
bundle install
rails generate auditron:install
rails db:migrateThat's it. Now tell Auditron who's making changes (see Controller setup) and add auditable to a model.
Configuration
Create an initializer at config/initializers/auditron.rb:
Auditron.configure do |config|
# Fields to never log across all models
config.ignored_fields = %i[updated_at created_at]
# Include request IP in every log entry (default: false)
config.store_ip = true
# Auto-purge logs older than N days (default: nil, keep forever)
# Call Auditron::Sweeper.purge! from a scheduled job
config.retention_days = 90
endHeads up: don't set
current_actorin the initializer. Instance variables like@current_useronly exist during a request, so they aren't available there. See Controller setup below.
Controller setup
Auditron needs to know who is making changes. Set the current actor in your controller after authentication. It works with any auth system.
Auditron stores the actor in a thread-safe variable and clears it automatically after every request, so it's safe for Puma and any threaded server.
JWT (API apps)
class ApplicationController < ActionController::API
private
def authenticate_account!
token = request.headers["Authorization"]&.split(" ")&.last
payload = JsonAuthToken.decode(token)
@current_user = Account.find_by(id: payload[:account_id])
render json: { error: "Invalid token" }, status: :unauthorized and return unless @current_user
# Set the actor. Auditron reads this on every model change.
Auditron.current_actor = @current_user
end
endDevise
class ApplicationController < ActionController::Base
before_action :set_audit_actor
private
def set_audit_actor
Auditron.current_actor = current_user
end
endSession based (no Devise)
class ApplicationController < ActionController::Base
before_action :set_audit_actor
private
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
def set_audit_actor
Auditron.current_actor = current_user
end
endService objects or background jobs
# Set and clear manually. Auditron does not clear this automatically
# outside of a request cycle.
Auditron.current_actor = admin_user
account.update!(status: "suspended")
Auditron.current_actor = nilGood to know: on signup or any unauthenticated request,
actor_typeandactor_idwill benil. That's correct. The user doesn't exist yet, so there's no actor to record.
Usage
Track all changes on a model
class Account < ApplicationRecord
include Auditron::Auditable
auditable
endTrack only specific fields
class Account < ApplicationRecord
include Auditron::Auditable
auditable only: [:email, :role, :status]
endExclude specific fields
class Account < ApplicationRecord
include Auditron::Auditable
auditable except: [:last_sign_in_at, :login_count]
endAttach custom metadata to a log entry
Pass any extra context you want stored alongside the log:
# Simple reason
account.audit_with(reason: "user requested name change").update!(first_name: "Jane")
# Support ticket reference
account.audit_with(
reason: "admin override",
ticket: "SUPPORT-1234",
note: "user forgot old email"
).update!(email: "new@example.com")
# GDPR deletion
account.audit_with(reason: "GDPR deletion request").destroyMetadata is stored as JSON and returned as a Hash:
account.audit_logs.last.metadata
# => { "reason" => "admin override", "ticket" => "SUPPORT-1234" }Querying audit logs
# All logs for a specific record
account.audit_logs
# Same, via class method
Auditron::AuditLog.for(account)
# All changes made by a specific actor
Auditron::AuditLog.by(admin)
# Filter by action
Auditron::AuditLog.action(:updated)
Auditron::AuditLog.action(:deleted)
Auditron::AuditLog.action(:created)
# Filter by time
Auditron::AuditLog.since(1.week.ago)
Auditron::AuditLog.since(1.month.ago)
# Chain them
Auditron::AuditLog.by(admin).action(:deleted).since(1.week.ago)
# Get the actor object directly from a log
log = account.audit_logs.last
log.actor # => full Account/User/Admin object
log.actor_type # => "Account"
log.actor_id # => 1
# Human readable summary
log.summary
# => "Account #8 was updated by Account #1"Log retention (Sweeper)
No other major audit gem ships with built-in log retention. Configure it once and run it from any scheduled job:
# config/initializers/auditron.rb
config.retention_days = 90 # keep logs for 90 days
# Call from a scheduled job (Sidekiq, GoodJob, cron)
Auditron::Sweeper.purge! # deletes all logs older than retention_daysExample with a background job:
class AuditLogCleanupJob < ApplicationJob
def perform
Auditron::Sweeper.purge!
end
endWhat a log entry looks like
Every AuditLog record contains:
| Field | Type | Description |
|---|---|---|
auditable_type |
String | Model class name, e.g. "Account"
|
auditable_id |
Integer | Record ID |
action |
String |
created, updated, or deleted
|
changed_fields |
JSON | Only changed fields, with before/after values |
actor_id |
Integer | ID of the actor who made the change |
actor_type |
String | Class name of the actor, e.g. "Account"
|
ip_address |
String | Request IP (when store_ip is on) |
metadata |
JSON | Custom data attached via audit_with
|
created_at |
DateTime | When the change happened |
Example
#<Auditron::AuditLog
id: 7,
auditable_type: "Account",
auditable_id: 8,
action: "updated",
changed_fields: { "first_name" => ["John", "Jane"] },
actor_type: "Account",
actor_id: 1,
ip_address: "::1",
metadata: { "reason" => "user requested name change" },
created_at: Sat, 04 Apr 2026 13:38:08 UTC
>How it compares
An honest comparison. Every gem has strengths, so pick the right tool for your use case.
| Feature | Auditron | PaperTrail | Audited | Logidze |
|---|---|---|---|---|
| Storage model | Diff only | Full snapshot | Diff (changes) | Diff (JSONB) |
| PostgreSQL | โ | โ | โ | โ |
| MySQL | โ | โ | โ | โ |
| SQLite | โ | โ | โ | โ |
| Built-in retention | โ | โ | โ | โ |
| Custom metadata | โ clean DSL | โ ๏ธ via meta config |
โ ๏ธ limited | โ |
| Actor tracking | Thread-local, set anywhere |
whodunnit (controller) |
current_user (controller) |
Custom |
| JWT / API friendly | โ | โ ๏ธ needs workaround | โ ๏ธ needs workaround | โ ๏ธ moderate |
| Background job support | โ set manually | โ ๏ธ manual wiring | โ ๏ธ manual wiring | โ ๏ธ manual |
| Chainable query DSL | โ | โ raw AR queries | โ raw AR queries | โ raw JSON ops |
| Rails required | โ ๏ธ ActiveRecord required | โ Rails required | โ Rails required | โ Rails required |
| Performance (large data) | โ ๏ธ unverified | โ heavy (full snapshots) | โ ๏ธ medium | โ optimized (JSONB) |
| Maturity | ๐ new | โ battle-tested | โ battle-tested | โ stable |
Choose Auditron when:
- You're building an API-first app with JWT or token-based auth
- You want built-in log retention without writing your own cleanup
- You'd rather have a clean query DSL than raw ActiveRecord queries
- You set the actor from service objects or background jobs
Choose PaperTrail when:
- You need full version history and the ability to revert records
- You're on a traditional session-based Rails app
- You want a battle-tested, widely supported gem
Choose Logidze when:
- You're on PostgreSQL and need maximum query performance
- You want diff storage backed by native JSONB operations
Compatibility
- Ruby
>= 3.0 - ActiveRecord
>= 7.0 - Rails
>= 7.0(optional, works with ActiveRecord outside Rails) - PostgreSQL, MySQL, SQLite
Also by the same author: Respondo ๐ฏ
Building a Rails API? Auditron pairs nicely with Respondo, which gives every JSON response the same shape (success, message, data, meta) with one-line helpers like render_ok and render_created.
Audit your changes with Auditron, standardize your responses with Respondo.
Contributing
Bug reports and pull requests are welcome on GitHub.
git clone https://github.com/spatelpatidar/auditron
cd auditron
bundle install
bundle exec rspecFound a bug or have a feature idea? Open an issue โ
License
Released under the MIT License.
If Auditron saved you from a "who changed this?" mystery, give it a โญ on GitHub. It helps other developers find it.