Reputation: 7900
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
Upvotes: 5
Views: 1732
Reputation: 4439
I think you hit upon a good solution with using the raw text instead of (or in addition to) the IDs. If you're storing usernames and addresses, it's probably not going to break any storage capacity. This is pretty much how a log file does it; it spits out who did what and when it happened. I don't think it would hamper your db at all to store some extra data in text. If it did get massive, however, you could always just keep the last few weeks' worth of logs. Maybe have some sort of job that cleans out any log files that are older than 4 weeks. But that mostly depends on the level of logging your'e doing: if you're logging every single action a user does, then this gets really big. If you're just interested in the key events (user logged in/out, created/updated/deleted something, etc...) then I think you're fine storing the text and cleaning out every once in a while.
Upvotes: 1
Reputation: 9226
Take a look at the gems listed on ActiveRecord Versioning, it's probably what you need.
Upvotes: 3