简体   繁体   中英

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. Eg '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

看看ActiveRecord版本控制中列出的宝石,它可能就是您所需要的。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM