Configuring centralized logging from Ruby on Rails apps

To send Ruby on Rails request logs, either:

We recommend remote_syslog2 because it works for other text files (like nginx and MySQL), has no impact to Rails app, and is easy to setup.

Also see Controlling Verbosity.

Method A: Use Papertrail’s tiny remote_syslog2 daemon

1. Install remote_syslog2

Download the current release. To extract it and copy the binary into a system path, run:

$ tar xzf ./remote_syslog*.tar.gz
$ cd remote_syslog
$ sudo cp ./remote_syslog /usr/local/bin

RPM and Debian packages also available.

2. Configure

Paths to log file(s) can be specified on the command-line, or save log_files.yml.example as /etc/log_files.yml. Edit it to define:

The remote_syslog2 README has complete documentation and more examples.

3. Start

Start the daemon:

$ sudo remote_syslog

Logs should appear in Papertrail within a few seconds of being written to the on-disk log file. Problem? See Troubleshooting.

remote_syslog requires read permission on the log files it is monitoring.

4. Auto-start

remote_syslog2 can be automated to start at boot using init scripts (examples) or your preferred daemon invocation method, such as monit or god. See remote_syslog --help or the full README on GitHub.

Troubleshooting

See remote_syslog2 troubleshooting.



Method B: Use the Remote Syslog Logger gem

1. Install Remote Syslog Logger

The easiest way to install remote_syslog_logger is with Bundler. Add remote_syslog_logger to your Gemfile.

If you are not using a Gemfile, run:

[sudo] gem install remote_syslog_logger

2. Configure Rails environment

Change the environment configuration file to log via remote_syslog_logger. This is almost always in config/environment.rb (to affect all environments) or config/environments/<environment name>.rb, such as config/environments/production.rb (to affect only a specific environment). Add this line:

config.logger = RemoteSyslogLogger.new('logsN.papertrailapp.com', XXXXX)

You can also specify a program name other than the default rails:

config.logger = RemoteSyslogLogger.new('logsN.papertrailapp.com', XXXXX, :program => "rails-#{RAILS_ENV}")

where logsN and XXXXX are the name and port number shown under log destinations.

Alternatively, to point the logs to your local system, use localhost instead of logsN.papertrailapp.com, 514 for the port, and ensure that the system’s syslog daemon is bound to 127.0.0.1. A basic rsyslog config would consist of the following lines in /etc/rsyslog.conf:

$ModLoad imudp
$UDPServerRun 514

3. Verify

To send a test message, start script/console in an environment which has the syslog config above (for example, RAILS_ENV=production script/console). Run:

RAILS_DEFAULT_LOGGER.error "Salutations!"

The message should appear on the system’s message history within 1 minute.


Verbosity

For more information on improving the signal:noise ratio, see the dedicated help article here.

Lograge

We recommend using lograge in lieu of Rails’ standard logging. Add lograge to your Gemfile and smile.

Log user ID, customer ID, and more

Use lograge to include other attributes in log messages, like a user ID or request ID. The README has more. Here’s a simple example which captures 3 attributes:

class ApplicationController < ActionController::Base
  before_filter :append_info_to_payload

  def append_info_to_payload(payload)
    super
    payload[:user_id] = current_user.try(:id)
    payload[:host] = request.host
    payload[:source_ip] = request.remote_ip
  end
end

The 3 attributes are logged in production.rb: with this block:

config.lograge.custom_options = lambda do |event|
  event.payload
end

The payload hash populated during the request above is automatically available as event.payload. payload automatically contains the params hash as params.

Here’s another production.rb example which only logs the request params:

config.lograge.custom_options = lambda do |event|
  params = event.payload[:params].reject do |k|
    ['controller', 'action'].include? k
  end

  { "params" => params }
end

Control Verbosity in logging

Additional resources for controlling verbosity


Troubleshooting

Colors and/or ANSI character codes (like 33[1m33[) appear in my log messages

By default, Rails generates colorized log messages for non-production environments and monochromatic logs in production. Papertrail renders any ANSI color codes it receives (see More colorful logging with ANSI color codes), so you can decide whether to enable this for any environment.

To enable or disable ANSI logging, change this option in your environment configuration file (such as config/environment.rb or config/environments/staging.rb). The example below disables colorized logging.

Rails 3.x:

config.colorize_logging = false

Rails 2.x:

config.active_record.colorize_logging = false

See: http://guides.rubyonrails.org/configuring.html#rails-general-configuration