How to Create a Full Audit Log in Rails for Every Table

How can I log an audit trail to the database?

If you set the log level in production to :debug then every request is logged with the information you need. Perhaps, it might be easier to write a parser for the debug production log.

If you prefer, you can also add a filter to your ApplicationController:

class ApplicationController
before_filter :log_activity

def log_activity
if Rails.env == 'production'
@logger ||= Logger.new("#{Rails.root}/log/activity.log")
values = [
request.remote_ip,
controller_name,
action_name,
params.inspect
]
@logger.info "#{values.join ' | '}"
end
end

...
end

this might not be complete as you want the output to be more verbose, but this is the idea.

How do I log particular events from Rails to a log file?

Thanks to everyone for the responses.

Casper, I did decide to build something custom.

I see your point with writing to a local db anyway, but the requirement for this project is to dump log files so a more elaborate log parsing service can interrogate the files and even combine them with information from other sources.

In order to get logging from both models and controllers, I ended up making a module that I would include in both the observers and ApplicationController.

The module looks a little something like this:

module MyEventLogger
mattr_accessor :logged_current_user
mattr_accessor :logged_remote_ip

def log_event(message)
@@logger ||= Logger.new(Rails.root.join('log', 'audit.log'))
@@logger.info "#{Time.now} | #{logged_current_user}@#{logged_remote_ip} | #{message}"
end

def logged_current_user
@@logged_current_user || "SYSTEM"
end

def logged_remote_ip
@@logged_remote_ip || "NO IP ADDRESS"
end
end

ApplicationController would have:

include MyEventLogger
before_filter :setup_logger

...

def setup_logger
MyEventLogger.logged_current_user = current_user
MyEventLogger.logged_ip_address = request.remote_ip
end

The observer would just have to include MyEventLogger and it would have access to the log_event method and the current user and ip address. For example:

class UserObserver < ActiveRecord::Observer
include MyEventLogger

def after_save(user)
log_event "The User #{user} was saved by #{logged_current_user}"
end

end

How do I view Rails audit records in my Rails console?

It didn't cross my mind to actually query the audits table and I had to look at the source code to see how to do it.

Generally this should work across versions:

Audited.audit_class.count # => 9999

With audited > 4.2.2, Audit got namespaced under Audited and it inherits from ActiveRecord::Base, here is the source code.

Audited::Audit.count # => 99999

How to capture audit history on ActiveRecord tables

Ruby Toolbox is the place to go for the choices and these are your choices for ActiveRecord audit gems

Rails audit system with both ActiveResource and ActiveRecord

The acts_as_audited gem should work well for you:

https://github.com/collectiveidea/acts_as_audited

And as far as ActiveResource is considered, it will also be a model in some other application. You can use the gem at the server side, and you don't need to audit it at the client side. All the CRUD operations using ActiveResource would finally translate to CRUD operations on the ActiveRecord (on server side).

So probably you need to look at it from a distance, and the same solution would apply in both the cases, but at different places.

Rails 4 + audited: Get audits for deleted object

If you know the id of the user, you can try:

Audited::Adapters::ActiveRecord::Audit.where(auditable_type: 'User', auditable_id: user_id)

For specific actions like create, update, destroy, you can try their scopes - creates, updates, destroys. I found it on their github repo.

Rails - Save tenant in Audited (formerly acts_as_audited)

I have recently added Acts As Tenant gem to a Rails app that is running the Audited gem. I was running into the same problem. I had added

acts_as_tenant :account

to the Audit model but it didn't do anything. I learned that you can't override in the Audit model but have to create a custom audit model that inherits from it. So I created the model:
custom_audit.rb

class CustomAudit < Audited::Audit
acts_as_tenant :account
end

I then added the initializer file audited.rb in confi/initializers like so:

Audited.config do |config|
config.audit_class = CustomAudit
end

I was still having the problem where all my multitenancy was working except the show_audit view. I finally deleted all of my audits from both tenants in my test setup. It worked! I can now add new audits and they scope just fine. But I still need to merge the actual client DBs into one, and I don't want to lose the history in the audit table... Not sure how to fix that.

So when I try to access the Audits it fails with current_tenant being nil. Not sure why deleting all of the current records in the table fixes it, but I need to find a way around it.



Related Topics



Leave a reply



Submit