Treasure Data logging library for Rails

Getting Started

Add the following line to your Gemfile:

gem 'td-logger'

For Rails 2.x (not tested) without Bundler, edit environment.rb and add to the initalizer block:

config.gem "td-logger"

And then add config/treasure_data.yml file as following:

# logging to Treasure Data directly
development:
  apikey: "YOUR_API_KEY"
  database: myapp
  access_log_table: access
  auto_create_table: true

# logging via td-agent
production:
  agent: "localhost:24224"
  tag: td.myapp
  access_log_table: access

# disable logging
test:

Logging access logs

Following information will be logged automatically:

  • controller name (‘controller’ column)

  • action name (‘action’ column)

  • remote host ip address (‘remote_addr’ column)

  • http status code (‘status’ column)

  • http referer (‘refer’ column)

To add information to the log, call ‘td_access_log’ method in a Controller:

class YourController < ApplicationController
  def myaction
    td_access_log[:column] = "value"
  end
end

Or call ‘TreasureData.access_log’ method outside of a Controller:

class MyHelperModule
  def mymethod
    TreasureData.access_log[:column] = "value"
  end
end

Logging model changes

Add ‘td_enable_model_tracer’ line to your Model class:

class MyModel < ActiveRecord::Base
  td_enable_model_tracer :mytable
end

It logs all columns when the model is changed. Use :only option to limit columns to log:

class MyModel < ActiveRecord::Base
  td_enable_model_tracer :mytable, :only => [:id, :mycol]
end

Alternatively, use :except option to except columns to log:

class MyModel < ActiveRecord::Base
  td_enable_model_tracer :mytable, :except => [:created_at]
end

Add ‘static’ option to add constant key-value pairs:

class MyModel < ActiveRecord::Base
  td_enable_model_tracer :mytable, :except => [:created_at], :static => {'model'=>'my'}
end

Logging other logs

You can log anytime using ‘TreasureData.log’ method:

class MyClass
  def mymethod
    TreasureData.log('mytable', {:col1=>"val1", :col2=>"val2"}
  end
end
Copyright

Copyright © 2011 Treasure Data Inc.

License

Apache License, Version 2.0