本文介绍了Log4j不会在删除时重新创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Tomcat中有一个使用log4j进行日志记录的Web应用程序。

如果我在Web应用程序运行时删除了日志文件,则不会重新创建文件?

怎么能我将log4j配置为在删除时重新创建文件而不必重新启动Tomcat?

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?

推荐答案

如果你的tomcat在linux服务器上,你使用对日志文件夹没有执行权限的特定用户启动它,你的log4j将不会重新创建你的日志,因为它可能只有读/写权限。

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.

如果是这种情况,请尝试a:

If this is the case try a:

chmod 755 在包含文件夹

编辑:

第二种可能性是某些操作系统仅在文件不再使用时才完成删除操作。如果是这种情况,你的tomcat仍然可以看到那里的日志。

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那个案例做了一个cron作业,每隔几分钟检查文件是否在那里。如果不是只是重新创建它。我会在几分钟内提供解决方案。

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.

所以应该在你的crontab中的bash会有类似的东西:

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

这篇关于Log4j不会在删除时重新创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 04:32