Send Ruby AWS SDK logs to remote syslog and Papertrail - Papertrail Blog

The revolution will be verbosely {,b}logged

Send Ruby AWS SDK logs to remote syslog and Papertrail

Posted by Troy Davis on

In the AWS Ruby blog, Trevor Rowe covers Logging HTTP Wire Traces with the AWS SDK for Ruby (aws-sdk gem) and overriding the default Logger class.

By default, aws-sdk logs to $stdout for Ruby apps or Rails.logger for Rails apps. Trevor’s example passes in an alternative Logger instance that is backed by $stderr:

AWS.config(:logger => Logger.new($stderr))

Let’s send these logs to a syslog destination like Papertrail, using Papertrail’s open-source remote_syslog_logger gem.

remote_syslog_logger

remote_syslog_logger provides a Logger-compatible class that’s backed by a UDP remote syslog destination (rather than an instance of IO, such as $stderr). To use it, add remote_syslog_logger to your Gemfile (or require it), then instantiate RemoteSyslogLogger with the syslog destination hostname and optionally, port. For example:

AWS.config(:logger => RemoteSyslogLogger.new('syslog.domain.com', 1234)

See Papertrail’s Add Systems page for these settings.

This works without any other system-wide config changes. A RemoteSyslogLogger instance can be passed to any Ruby class which expects a Logger instance.

Customize

By default, RemoteSyslogLogger will use its running program as the program name in the syslog message. Often this is just rails. Let’s set the program to something more descriptive, like myapp-development. Let’s also enable the AWS Ruby SDK http_wire_trace option to output HTTP headers (which one would typically only do in development). Here it is:

AWS.config(:logger => RemoteSyslogLogger.new('syslog.domain.com', 1234, :program => 'myapp-#{RAILS_ENV}'), :http_wire_trace => true)

The README has more.

Extend

To use two Loggers, one could wrap RemoteSyslogLogger within a container class which simply invoked each of them.

Other ways to accomplish a similar result include writing logs to local syslog and configuring it to transmit them, or write to a text log file and use a daemon to watch and send it (see rsyslog, syslog-ng, or remote_syslog).

Happy debugging!