One of the benefits of switching your Rails app to use SysLog is taking advantage of your OS’s system-wide logging, as well as having finer grained control over how to process your log messages. For example, you can split the logs up based on Rails app, mongrel process, or even by app server if you’re using remote syslog.
We’ll use Eric Hodel’s SyslogLogger Ruby Logger drop-in to get our app talking to SysLog.
Firstly, we’ll chuck the SyslogLogger library into our Rails app’s vendor directory:
cd vendor
curl http://rubyforge.org/frs/download.php/20520/SyslogLogger-1.4.0.tgz -L | tar -zxvv
We then need to set it up in environment.rb (or a specific environment file such as production.rb). We need to add its lib directory to the load path and set it as the DEFAULT_RAILS_LOGGER:
# Use SysLogger rather than the default logger.
#
# see http://seattlerb.rubyforge.org/SyslogLogger/
# for example /etc/syslog.conf setups
$:.unshift File.join(RAILS_ROOT, "vendor", "SyslogLogger-1.4.0", "lib")
require 'syslog_logger'
config.logger = RAILS_DEFAULT_LOGGER = SyslogLogger.new('my_rails_app')
To test it we’ll load up script/console and give it a whirl:
>> RAILS_DEFAULT_LOGGER.error "Hello my lil SysLog"
and check our log, /var/system.log in OSX’s case, to see the results :
Jun 6 16:30:11 tim my_rails_app[2923]: Hello my lil SysLog
woohoo!
...and now we’re using SysLog we can use Eric’s other tool, ProductionLogAnalyzer, to identify performance bottleknecks of our application. Geoffrey Grosenbach (aka topfunky) wrote a Hodel3000CompliantLogger if you want to use this tool without SysLog).
Read more about SysLog on your SysLog daemon’s man page (on OSX check out the man pages for syslog, syslogd and syslogd.conf).
Archived comments
Comments were previously allowed on articles. Though no new comments are being accepted you can see the old comments below.
-
Can you specify which logger to use? ie: send to local7, etc.
-
Jez: Yep, you specify that in
SyslogLogger.new('my_rails_app')... so in my case it’s setting the sender to be “my_rails_app” -
Using this with metalog and I’m sure any drop in replacement will work too.
There’s also Analogger available if you want to use something other than the default.
-
hey guys, I was wondering if anyone came up with a way to get around the collapsing of return characters when msgs are logged to SysLog. At least that is what occurs on CentOS.
reading formatted msgs rather than text sprinkled with ’\r\n’ is much easier on the eyes.
thanks! Adam
-
My app uses one codebase to handle requests from different vendors. I would like to separate the log file so that each vendor gets their own log file. How can I do that? TIA.
-
Bala: I don’t think it’s possible to do that on a per-request basis. Maybe have different mogrels for different vendors, implement your own logging or do some log analysis?
-
Tim: Actually, Jez was asking if you can specify a custom facility (which is different than the program name) for use by Syslog. SyslogLogger 1.4 doesn’t allow you to do this, instead relying on Syslog to use its default “user” facility. I’ve submitted a patch for this, though, so hopefully it will be addressed.
