Loading...

Logrotate Java Application Logs

Running a professional Java EE application with JBoss EAP/Wildfly or any other application server involves a good housekeeping strategy for log files. Pay attention that most developers make the mistake to log information twice, hence in most scenarios the default texturation is kept. So the log information will be written into the console.log and server.log. Furthermore if you don’t use a files size rotator, the server.log will be rotated daily. The potential threat is, if you write the logs to separate SAN partition with limited disk space, you might crash the application, if he has no disk space left. To prevent this, one possibility is to use logrotate on Linux for the job.

My applications run in the docker container. To prevent extreme disk usage, server.log and console.log is rotated when 2 GB is reached, 7 rotations mean that he keeps max 14 GB, after 3 days the log files are deleted.

# file size rotation of server.log to prevent full disk space
/var/log/docker/*/server.log {
        su root cinhtau
        # logrotate automatically by file size 2 GB
        size 2G
        create 0600 cinhtau cinhtau
        # keep only last 7 rotations
        rotate 7
        copytruncate
        compress
        missingok
        maxage 3
}
/var/log/docker/*/console.log {
        su root cinhtau
        # logrotate automatically by file size 2 GB
        size 2G
        create 0600 cinhtau cinhtau
        # keep only last 7 rotations
        rotate 7
        copytruncate
        compress
        missingok
        maxage 3
}

Regular daily rotated server logs are compressed and deleted after 3 days.

# jboss server daily rotated logs
/var/log/docker/*/server.log.????-??-?? {
    su root cinhtau
    compress
    nocreate
    nodateext
    ifempty
    missingok
    rotate 1
    size 0
    start 0
    lastaction
        # Remove rotated files older than 3 days
        find /var/log/docker -name 'server.log.????-??-??-??.0.gz' -mtime +3 -exec rm {} \;
    endscript
}

To run the texturation (filename docker_jboss)

/usr/sbin/logrotate /etc/logrotate.d/docker_jboss

To check if the file are rotated

cat /var/lib/logrotate.status

Lets look at an example. Following directory contains before logrotation:

cinhtau@alpha:/var/log/docker/jboss# ls -l
-rw-r--r-- 1 cinhtau cinhtau    3397 Dec  1 10:50 backupgc.log.current
-rw-r--r-- 1 cinhtau cinhtau     356 Dec  2 10:42 fetrace.log
-rw-r--r-- 1 cinhtau cinhtau    6754 Dec  2 12:47 gc.log.0.current
-rw-r--r-- 1 cinhtau cinhtau 1240319 Dec  2 13:30 server.log
-rw-rw-r-- 1 cinhtau cinhtau 7157923 Nov 29 23:57 server.log.2016-11-29
-rw-rw-r-- 1 cinhtau cinhtau 4624286 Nov 30 15:54 server.log.2016-11-30
-rw-r--r-- 1 cinhtau cinhtau 7510470 Dec  1 23:58 server.log.2016-12-01

After log rotation:

cinhtau@alpha:/var/log/docker/jboss# ls -l
-rw-r--r-- 1 cinhtau cinhtau    3397 Dec  1 10:50 backupgc.log.current
-rw-r--r-- 1 cinhtau cinhtau    6754 Dec  2 12:47 gc.log.0.current
-rw-r--r-- 1 cinhtau cinhtau 1251043 Dec  2 13:46 server.log
-rw-rw-r-- 1 cinhtau cinhtau  426809 Nov 29 23:57 server.log.2016-11-29.0.gz
-rw-rw-r-- 1 cinhtau cinhtau  218025 Nov 30 15:54 server.log.2016-11-30.0.gz
-rw-r--r-- 1 cinhtau cinhtau  405076 Dec  1 23:58 server.log.2016-12-01.0.gz

An example for file size rotation.

cinhtau@alpha:/var/log/docker/jboss# ll
-rw-r--r-- 1 cinhtau cinhtau 215609425 Dec  2 14:02 server.log
-rw-r--r-- 1 cinhtau cinhtau 315519951 Dec  2 13:55 server.log.1.gz
-rw-rw-r-- 1 cinhtau cinhtau    143792 Nov 28 23:58 server.log.2016-11-28.0.gz
-rw-rw-r-- 1 cinhtau cinhtau    177975 Nov 29 23:59 server.log.2016-11-29.0.gz
-rw-rw-r-- 1 cinhtau cinhtau  35635494 Nov 30 23:59 server.log.2016-11-30.0.gz
-rw-r--r-- 1 cinhtau cinhtau 206083051 Dec  1 23:59 server.log.2016-12-01.0.gz