0.0
The project is in a healthy, maintained state
AuditLog is a lightweight gem for adding create/update/delete auditing to your Rails models. Tracks changes, actor, and optional reasons.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

>= 6.0
 Project Readme

Gem Version

AuditLog

A lightweight, Rails-friendly audit logging gem to track model changes (create, update, destroy) with contextual metadata like actor and reason. Perfect for admin panels, SaaS apps, and audit trails.


✨ Features

  • Track create, update, and destroy events on any model
  • Store changes in an audit_log_entries table
  • Associate logs with the user (or any actor) who made the change
  • Optional reason for change
  • Configurable only: and except: fields
  • Easy to install and integrate

πŸ”§ Installation

Add this line to your application's Gemfile:

gem "audit_log_vk"

Then run:

bundle install

Install the initializer and migration:

bin/rails generate audit_log:install
bin/rails db:migrate

πŸš€ Usage

1. Include the concern in your model:

class Post < ApplicationRecord
  include AuditLog::Model

  audited
end

You can optionally limit or exclude tracked attributes:

audited only: [:title, :status]         # Only track these fields
audited except: [:updated_at, :synced]  # Track everything but these

2. Set the actor (who made the change):

In your controller (e.g. ApplicationController):

before_action do
  AuditLog::Context.actor = current_user
end

You can configure the method name (current_user) in the initializer.

3. Optionally set a reason:

You can set a reason for changes anywhere in your code:

AuditLog::Context.with(reason: "bulk import") do
  post.update!(status: "archived")
end

βš™οΈ Configuration

Generated initializer at config/initializers/audit_log.rb:

AuditLog.configure do |config|
  config.actor_method = :current_user
  config.ignored_attributes = ["updated_at"]
end

πŸ§ͺ Testing in your app

Use AuditLog::Entry to inspect audit logs:

AuditLog::Entry.for_model(post).order(created_at: :desc)

πŸ“¦ Entry Table Schema

The install generator includes a migration that creates:

create_table :audit_log_entries do |t|
  t.string  :auditable_type
  t.bigint  :auditable_id
  t.string  :action
  t.json    :changed_data
  t.string  :reason
  t.string  :actor_type
  t.bigint  :actor_id
  t.timestamps
end

βœ… Development

This gem uses rspec and an in-memory SQLite DB to test audit hooks.

Run specs with:

bundle exec rspec

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/audit_log. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

πŸ“„ License

The gem is available as open source under the terms of the MIT License.

πŸ™Œ Credits

Created with by Viktor AraΓΊjo.

Inspired by open-source audit trail gems like PaperTrail, Audited, and others β€” but lighter and more simple.