I am thinking of using syslog in my rails applications. The process is outlined in this blog post:
- Add
gem 'SyslogLogger'
to yourGemfile
- Add
require 'syslog_logger'
to the top ofconfig/environments/production.rb
- Also uncomment the
config.logger =
line in the same file.
In production box I have 4 rails applications running using passenger. If I switch to use syslogger for all 4 of my applications then I am afraid that the log messages from all 4 applications will go to a single file and the log messages will be interleaving. Of course, I can use splunk but first I wanted to check if it was possible for me to get one log file for each of my rails application. That would be desirable for my situation.
Is that possible?
Yes, by default, almost all Unix
syslogds
will write messages given in theuser
orlocal*
facility in the same file. However, everysyslogd
I know will allow you to specify logfiles on a per-facility basis, so you can have your first application log tolocal1.*
, second one tolocal2.*
and so on.Furthermore, newer syslog daemons like
syslog-ng
allow for splitting messages to different files by evaluating the message against a regular expression (write log strings which haverailsapp_1
in them to/var/log/railsapp_1.log
and so on).So, configure your
syslogd
appropriately and you are done (the gory details of changing that configuration should be asked on serverfault.com if your system's man pages don't help you doing it.)@cite's answer covers one option for distinguishing the apps. However, the syslog message framing actually has 2 fields that make it even easier:
hostname
andtag
(more commonly known and used as program name).hostname
is set by the system syslog daemon before it forwards the message to a centralized server. It will be the same for all apps on the same system but may be handy as you grow past 1 server.The more interesting one is
tag
. Your app definestag
when it instantiatesSyslogLogger
. For example:The logger class will send to the system syslogd as
app1
, and that appear in both the local log file and any remote syslog destinations (without needing to modify the log message itself). The default israils
. All modern syslog daemons can filter based ontag
; seeprogram()
for syslog-ng and$programname
for rsyslog.Also, it's worth noting that
SyslogLogger
is basically wrapping the Copenlog()
andsyslog()
functions, so basically all post-log configuration happens on the system daemon. Generally that's desirable, but sometimes you may want your Rails app to log directly to a specific destination (like to simplify automated deployment, change attributes not allowed bysyslog()
, or run in environments without access to the system daemon).We ran into a couple of those cases and made a drop-in
Logger
replacement that generates the UDP packets itself. Theremote_syslog_logger
gem is on GitHub.