How to rotate your application logs

How to rotate your application logs

Concept:
We use system's logrotate script to rotate our services logs. This (/etc/logrotate.conf) is a default script of the system which is executed by cronjob (/etc/cron.daily/logrotate) for log rotation. So, logration script and cronjob is already prepared by system, all we need to do is create a new file on (/etc/logrotate.d/) directory for our application and declair our requirement as below-

[root@db1 ~]# vim /etc/logrotate.d/asterisk
/var/log/asterisk/call.log {
    #If the log file is missing, go on to the next one without issuing an error message.
        missingok

    #rotate log files hourly/daily/weekly
        daily

    #Do not rotate the log if it is empty
        notifempty

    #Old versions of log files are compressed with gzip to save disk space.
        compress

    #create new (empty) log files after rotating old ones with ownership and password
        create 0664 root root
}

Note: Change the file name "asterisk" and log file location "/var/log/asterisk/call.log" as per your application.

Perameters:
weekly: Log files are rotated if the current weekday is less then the weekday of the last rotation or if more then a week has passed since the last rotation.
rotate 52: Log files are rotated 52 times before being removed or mailed to the address specified in a mail directive. If count is 0, old versions are removed rather then rotated.
compress: Old versions of log files are compressed with gzip to save disk space.
missingok: If the log file is missing, go on to the next one without issuing an error message.
notifempty: Do not rotate the log if it is empty
sharedscripts: Normally, prerotate and postrotate scripts are run for each log which is rotated, meaning that a single script may be run multiple times for log file entries which match multiple files. If sharedscript is specified, the scripts are only run once, no matter how many logs match the wildcarded pattern. However, if none of the logs in the pattern require rotating, the scripts will not be run at all.
postrotate
/bin/kill -HUP `cat /var/run/httpd.pid 2>/dev/null` 2> /dev/null || true
endscript: The lines between postrotate and endscript (both of which must appear on lines by themselves) are executed after the log file is rotated. These directives may only appear inside a log file definition.

For Documentation purpose, I have also given the default scripts here.
[root@db1 ~]# vim /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
        minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.

Cronjob scripts:
[root@db1 ~]# vim /etc/cron.daily/logrotate
#!/bin/sh

/usr/sbin/logrotate /etc/logrotate.conf >/dev/null 2>&1
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

Referance:
http://www.thegeekstuff.com/2010/07/logrotate-examples/
http://www.cyberciti.biz/faq/how-do-i-rotate-log-files/