Ruby on Rails
To send Ruby on Rails request logs, either:
- use Papertrail's tiny remote_syslog2 daemon to read an existing log file (like
production.log
), or - change Rails' environment config to use the remote_syslog_logger gem.
We recommend remote_syslog2 because it works for other text files (like nginx and MySQL), has no impact on the Rails app, and is easy to set up.
Also see Controlling Verbosity.
Send log file with remote_syslog2
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 are also available.
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:
- path to your Rails log file (such as
production.log
) and any other log file(s) that remote_syslog2 should watch. - the destination
host
andport
provided under log destinations. If no destination port was provided, sethost
tologs.papertrailapp.com
and remove theport
config line to use the default port (514).
The remote_syslog2 README has complete documentation and more examples.
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.
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.
Send events with the remote_syslog_logger
gem
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:
$ gem install remote_syslog_logger
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
Verify configuration
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
Troubleshooting
Colors and/or ANSI character codes 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
The scripts are not supported under any SolarWinds support program or service. The scripts are provided AS IS without warranty of any kind. SolarWinds further disclaims all warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The risk arising out of the use or performance of the scripts and documentation stays with you. In no event shall SolarWinds or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the scripts or documentation.