Wednesday, April 16, 2008

Rotate log files

Logging is come into picture when something went wrong in application and have to point out the root cause of the problem.

Some times application running out of disk space. This is not because of our code but because of log files generated by apps.

Logrotate is effective utility for manage files which grow rapidliy like catalina.out and etc....

To rotate ghe old log files we can use log4j in java ,log4r in Ruby. There is another cool way to Rotate the log in unix based servers.

There is a configuration file called logrotate.conf in /etc directory. Add the following code

# Rotate MyApps logs
1) /home/MyApps/log/*.log {
2)daily
3) missingok
4)rotate 5
5)compress
6)delaycompress
7)notifempty
8)copytruncate
9)create 0666 devgroup devgroup
}

This will take care of log file to rotate.

Explanation:
1) /home/MyApps/log/*.log {
Absolute path to log file of project
2)daily
rotate log files daily
3) missingok
If log file is missing dont raise any error.
4)rotate 5
Log files rotated <5> times
for Example the directory /log look like:
production.log
production.log.1
produciton.log.2.gz
produciton.log.3.gz
production.log.4.gz
5)compress
Old rotated file compressed to by default gzip
6)delaycompress
postpone compression of previous log file to next rotate
current log would be production.log and previous log would be
production.log.1 not production.log.1.gz
for Example the directory /log look like:
production.log
production.log.1
produciton.log.2.gz
produciton.log.3.gz
production.log.4.gz
7)notifempty
don't rotate if log file is empty
8)copytruncate
Truncate the original log file in place after creating a copy, instead of moving the old log file and optionally creating a new one.
9)create 0666 devgroup devgroup
The file permission, user and group name of the rotated files
file permission====> 0666
username =====> devgroup
groupname =====> devgroup

Go and start play with logrotate.

0 comments:

Post a Comment