Treasure Data logging library for Ruby / Rails
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Treasure Data logging library for Rails

Build Status Coverage Status


This gem is a logging library for Treasure Data. The events logged by this module will be uploaded into the cloud. There're two ways to upload:

  • direct upload from applications: app -> cloud. This option is easier to configure and setup, but it requires extra memory in the application processes. The data is logged directly in the Treasure Data Cloud through the REST APIs by leveraging the Treasure Data Ruby Client (td-client-ruby) library.

  • indirect upload from td-agent: app -> td-agent -> cloud. This option requires extra steps to install the daemons into your cluster, but lowers the logging impact on your applications. The data is sent to a td-agent running either locally or remotely by leveraging the Fluentd Ruby Logger (fluent-logger-ruby) library.

The actual upload method can be selected in the config file as described below.


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"


Direct Upload

This configuration enables the application to upload the events directly from the application processes (app -> cloud) through the Treasure Data REST APIs. Please add this template config/treasure_data.yml file and set your apikey:

  apikey: "YOUR_API_KEY"
  database: rails_development
  debug_mode: true  # enable debug mode

  apikey: "YOUR_API_KEY"
  database: rails_production
  debug_mode: false

# disable logging

If you would wish to keep your API key as an environment variable, you can use this setting:

apikey: <%= ENV['TD_API_KEY'] %>

Alternatively, one can initialize the Ruby Logger module for direct upload straight from within the application:'production',
                          :apikey => "YOUR_API_KEY",
                          :auto_create_table => true)

The hash key / value pairs specified in the second and following arguments of the 'open' function correspond to the configuration option of the underlying td-client-ruby Ruby Client library.

Refer to the Ruby SDK documentation for more information.

Please note that we recommend to use a write-only API key. To obtain one, please:

  1. Login into the Treasure Data Console at;

  2. Visit your Profile page at;

  3. Insert your password under the 'API Keys' panel;

  4. In the bottom part of the panel, under 'Write-Only API keys', either copy the API key or click on 'Generate New' and copy the new API key.

Indirect Upload

When the 'agent' and 'tag' options are specified, the events are logged into the local td-agent daemon. Later on, the daemon uploads the event periodically to the cloud (app -> td-agent -> cloud).

This requires additional setup steps but lowers the memory and performance impact to your application processes. For the installation process, please look at the section “How to Install Treasure Agent?” on “Quickstart Guide”.

NOTE: This configuration is not supported on PaaS platforms.

  agent: "localhost:24224"
  tag: td.myapp
  debug_mode: false

Alternatively, one can initialize the Ruby Logger module for indirect upload within the application:

                                :host => 'localhost',
                                :port => 24224)

The arguments of the 'open_agent' function correspond to the configuration option of the underlying Fluentd Ruby Logger (fluent-logger-ruby) library.

Logging events

You can log anytime using '' method:'table_name', {:foo=>:bar})

Rails extension

In rails application, you can use 'TD.event.attribute' to set static attribute to all events:

class ApplicationController
  def authenticate
    # set 'uid' attribute to succeeding event logs
    TD.event.attribute[:uid] = 198

class MyController < ApplicationController
  def mymethod
    # this event includes 'uid' attribute'table_name', {:foo=>:bar})

Test mode

This mode uses TestLogger of fluent-logger-ruby to store the events. If you want to enable this mode, then specify 'test_mode' in your configuration.

  apikey: "YOUR_API_KEY"
  database: rails_development
  debug_mode: true
  test_mode: true

You can access the logger instance via TD.logger to check the status.

TD.logger.queue #=> [{},{},..]

Further Readings

If you have any problem, please refer our document site.



Copyright © 2011 Treasure Data Inc.


Apache License, Version 2.0