Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep a log of user events in rails

I am currently trying to implement a log for the administration section of my rails application. This will allow an admin to see what actions a user has performed and when. E.g 'User added a new address', 'User updated their postcode from X to Y'.

As each action could potentially involve many models, not just the user, I have made a log model which has fields for all the other system models ids, a message and a log code.

class CreateLogs < ActiveRecord::Migration
  def self.up
    create_table :logs do |t|
      t.integer :user_id
      t.integer :marker_id
      t.integer :administrator_id
      t.integer :group_id
      t.integer :assignment_id
      t.integer :submission_id
      t.integer :code
      t.text :message

      t.timestamps
    end
  end

  def self.down
    drop_table :logs
  end
end

My concern is that for example, a user could (let's say) add an assignment to their account, it gets logged as

Log.create(:user_id => current_user.id, :assignment_id => the_assignment.id, :code => 342, :message => '')

(Somewhere the code 342 corresponds to 'User created a new address', hence no need for message)

Obviously in a log view, I can pull the relevant user and address info from the log ids/details but if this user or address were to be deleted, all that information would be unavailable and so looking back through the logs, the entry would be basically useless.

There has to be a better way or something already out there to help log system events like this and cope with potential deletions.

Alternatively I could store the entire entry as a text message but wouldn't that be really bad and fill up the database unnecessarily?

Let me know if any of that is unclear, just figured logging application actions/events has to have been done before!

Thanks,

Pete

like image 284
Pete Hamilton Avatar asked Oct 10 '22 03:10

Pete Hamilton


1 Answers

Take a look at the gems listed on ActiveRecord Versioning, it's probably what you need.

like image 110
eugen Avatar answered Oct 18 '22 00:10

eugen