acts_as_event_logger
logging made easy!
How easy?
Easy peasy, thats how easy. Using this plugin your models can log any type of data, from boring strings to complex objects, using a key/value method. Then, when the time is right, you can query your logs for rather complex statistics. One example could be:
User.count_logs_by_period('page_hit', :week, :start_time => Time.now-1.year, :end_date => Time.now)
This would return the total page hits, per week, from one year ago to today. For more details checkout usage.
Installation and Setup
Here’s how to get rolling:
-
Install the plugin via subversion
$ ./script/plugin install \
svn://rubyforge.org/var/svn/eventlogger/trunk/acts_as_event_logger -
Generate the needed database migration
$ ./script/generate acts_as_event_logger migration
-
Restart your server
Usage
For our example, lets pretend we are having safari guides log various animal stampedes ... serious business.
First lets give our safari guides the ability to log events:
class SafaiGuide < ActiveRecord::Base acts_as_event_logger end
So our guide Mobutu just witnessed a giraffe stampede of 50 giraffes:
mobutu = SafariGuide.find(:first, :conditions => {:name => 'Mobutu'} mobutu.log('giraffe stampede', 50)
But then Mobutu remembers he also saw a rhino stampede yesterday, but forgot to log it. No biggie Mobutu we got you covered, you can manually set the time of the event log:
mobutu.log('rhino stampede', 20, Time.now - 1.day)
So everything is good, all the guides love logging cause it's just so easy. But then one day Akello, the boss, hears that she has to compile all these stampede logs into statistics like rhino stampedes per hour for the past 7 days as reported by Mobutu, or gazelle stampedes every day for the past year as reported from all the safari guides. Don't flip Akello, we got you covered too:
mobutu.count_logs_by_period('rhino stampede', :hour, :start_time => Time.now-7.days) SafariGuide.count_logs_by_period('gazelle stampede', :day, :start_time => Tim.now - 1.year)