Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using rsyslogd in Rails 3.0

I am running a couple of Rails 3.0 sites on a Debian server. Currently all logs go into RAILS_ROOT/log/production.log (RAILS_ROOT for each site)

I would like to change this, so that all logging goes through rsyslog and is placed in:

/var/log/rails/site1.log
/var/log/rails/site2.log
etc.

And also to have these logs auto-rotated every day.

How is this done?

/ Carsten

like image 948
Carsten Gehling Avatar asked May 29 '12 07:05

Carsten Gehling


1 Answers

In the past I have used the syslog-logger gem for this

You can set this up in an initializer:

config.logger = Logger::Syslog.new("site1", Syslog::LOG_LOCAL5)

In recent ruby versions, there's also syslog/logger in the standard library - usage is pretty much the same.

To start logging to syslog rather than the default text file. What happens next is a syslog configuration thing - you need to create rsyslog rules that define where your data goes

The simplest possible thing would be something like

!site1 /var/log/site1.log

Which directs everything with the program name "site1" ( the first argument to Logger::Syslog).

There is lots more you can do, for example you could forward log messages across to a central logging server that aggregates them all into one file so tht you don't have Iook at one log file for each of your app instances.

For log rotation I use logrotate - I believe rsyslog can actually handle that sort of stuff internally but I don't know the details.

like image 92
Frederick Cheung Avatar answered Oct 11 '22 06:10

Frederick Cheung