Unix


I was working on a Rails application recently that required the logs to be sent to remote log server. This blog is just a summary of the steps to get it working. I used Rails 2.0 and the development was done on Ubuntu.

The first thing was to get syslog-ng. Its more advanced than syslogd.

sudo apt-get install syslog-ng

The next step was to get the SysLogLogger tool to send the logs to syslog instead of a file. Go to the vendor directory of your application and get SysLogLogger using the following command.

curl http://rubyforge.org/frs/download.php/20520/SyslogLogger-1.4.0.tgz
-L | tar -zxvv

Assuming that you are working in the development environment , add this to the start of the development.rb file in the configuration directory

$:.unshift File.join(RAILS_ROOT, "vendor", "SyslogLogger-1.4.0", "lib")
require 'syslog_logger'
RAILS_DEFAULT_LOGGER = SyslogLogger.new(program_name = 'my-application')
config.logger = RAILS_DEFAULT_LOGGER

Locate the syslog.conf file in the etc directory and add these lines.

!my-rails-application
user.* /var/log/development.log


What this does is gets the logs that are sent to syslog from the rails application and writes it to the development.log file in /var/log/ dir

To get the changes working, restart the syslog service on Ubuntu by running the following command
sudo /etc/init.d/sysklogd restart

To send the logs to a remote log server instead of the log file use this in the syslog.conf file instead
user.* @address of remote log server

The log server should be configured to accept logs from remote systems. The syslogd file in the remote log server needs to be changed to show the following for this purpose

SYSLOGD="-r"

The syslogd file is located in the etc/default dir in Ubuntu. Since I had the log server configured already, I did not need to change the configuration. Please add comments on this post in case something is missing.

The log files can be rotated on a periodic basis (or depending on size) by configuring the logrotate.conf file in the etc dir.
See man logrotate for details.

The date command on Unix displays or sets the system date or time. There are a variety of formatting options available.

$ date +"%m"

displays the month(01 to 12)

$ date +"%B"

gives the full month (e.g. January)

See the link for details.

I needed to get the abbreviated last month for one of my scripts. (e.g Nov). It seems that GNU date allows us to use the command as such :

$ date -d"1 month ago"

Sat Dec 1 12:36:42 MST 2007

So, I could get the abbreviated month by saying

$ date -d"1 month ago" +"%b"

It worked fine yesterday. But today(31 Dec) it says that the last month is Dec. When we say “1 month ago” , it tries to look for 31 Nov, and then corrects the date to 1 Dec. That doesn’t work for what I need.

So here is what I did

month=$(date +"%m")
if((${month}==1));then
month=12
else
let month=${month}-1
fi
last_month=`date -d"2007-"${month}"-1" +"%b"`

I get the numeric value of the current month and subtract one . In the last step I make a dummy date with the month I calculated and then use “%b” to get the abbreviated last month.

Powered by ScribeFire.