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?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
In log4j.properties, configure a RollingFileAppender
Configure a daily cron job (sh script in /etc/crond.daily/) that cleans logs over $DAYS old
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 folderEDIT:
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:
I found the solution for Log4j2.
Short:
We can manually initialize rollover process when detect file deletition.
Rollover can be initialized using
RollingFileManager
:Longer is here.
I went through the source code of
log4j
. When a FileAppender/RollingFileAppender is initialized, aFileOutputStream
instance is created pointing to the File. A newFileDescriptor
object is created to represent this file connection. This is the reason, the other solutions like Monitoring the file through Cron and Creating the File in append method by overriding didn't work for me, because a new file descriptor is assigned to the new file. Log4j Writer still points to the old FileDescriptor.The solution was to check if the file is present and if not call the activeOptions method present in FileAppender Class.
Finally add this to the log4j.properties file:
Note: I have tested this for log4j 1.2.17