Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j does not recreate files on deletion

I have a web application in Tomcat that uses log4j for logging.
If I delete the log files while the web application is running the files are not recreated?
How can I configure log4j to recreate the files on deletion without having to restart Tomcat?

like image 748
Jim Avatar asked Oct 09 '22 06:10

Jim


2 Answers

If your tomcat is on a linux server, and you start it with a specific user that doesn't have execute rights on the log folder, your log4j will not recreate your logs, because probably it has only read/write rights.

If this is the case try a:

chmod 755 on the containing folder

EDIT:

The second possibility is that some operating systems complete the "delete" operation only when the file is not in use anymore. If this is the case your tomcat can still "see" the log as there.

EDIT2:

In that case make a cron job that every several minutes checks if the file is there. If not just recreate it. I will provide a solution in a few minutes.

So the bash that should be in your crontab would have something like:

if [ ! -f /tomcat_dir/log4j.log ]
then
  `touch /tomcat_dir/log4j.log`;
fi
like image 120
Bogdan Emil Mariesan Avatar answered Oct 12 '22 21:10

Bogdan Emil Mariesan


In log4j.properties, configure a RollingFileAppender

#------------------------------------------------------------------------------
#
#  Rolling File Appender
#
#------------------------------------------------------------------------------
log4j.appender.rfile = org.apache.log4j.RollingFileAppender
log4j.appender.rfile.File = logs/server.log
log4j.appender.rfile.Append = false
log4j.appender.rfile.MaxFileSize=10240KB
log4j.appender.rfile.MaxBackupIndex=10
log4j.appender.rfile.layout = org.apache.log4j.PatternLayout
log4j.appender.rfile.layout.ConversionPattern = %d %-5p [%C] (%t) %m (%F:%L)%n

Configure a daily cron job (sh script in /etc/crond.daily/) that cleans logs over $DAYS old

find $LOG_ROOT/log/server.log* -mtime +$DAYS -exec rm {} \;
like image 37
Bruno Grieder Avatar answered Oct 12 '22 19:10

Bruno Grieder