[英]Log4j daily rolling catalina.out without restarting Tomcat?
i am having trouble with configuring Log4j correctly. 我正确配置Log4j时遇到问题。 I was expecting Log4j to rotate my catalina.out file at midnight when configuring it like the following..
我期待Log4j在午夜时将我的catalina.out文件旋转,如下所示进行配置。
log4j.properties: log4j.properties:
log4j.rootLogger=INFO, CATALINA
# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File=/var/log/tomcat7/catalina.out
log4j.appender.CATALINA.Append=true
log4j.appender.CATALINA.Encoding=UTF-8
# Roll-over the log once per day
log4j.appender.CATALINA.DatePattern='.'yyyy-MM-dd-HH-mm'.log'
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern =%d{HH:mm:ss} %5p [%t] - %m%n
After configuring I restarted Tomcat and everything is written to: 配置完成后,我重新启动了Tomcat,所有内容都写入:
/var/log/tomcat7/catalina.out
To test my configuration i changed the current date time to like 23:59:59: 为了测试我的配置,我将当前日期时间更改为23:59:59:
#ls -l /var/log/tomcat7/
-rw-r--r-- 1 tomcat7 tomcat7 5840 4. May 00:00 catalina.out
As you can see, it didnt rotate at midnight... (?) 如你所见,它在午夜没有旋转...(?)
When restarting Tomcat it works perfectly fine: 重新启动Tomcat时,它完全正常:
#ls -l /var/log/tomcat7/
-rw-r--r-- 1 tomcat7 tomcat7 5840 4. May 13:37 catalina.out
-rw-r--r-- 1 tomcat7 root 2395 4. May 00:00 catalina.out.*CURRENTDATE*.log
Is it even possible to rotate my logfiles without restarting Tomcat? 甚至可以在不重启Tomcat的情况下旋转我的日志文件吗?
Thanks in advance, Marley 谢谢,马利
There are three solutions for this problem: 这个问题有三种解决方案:
Benefits of solutions: 解决方案的好处:
Disadvantages: 缺点:
provides only simple backup with date. 仅提供带日期的简单备份。 Date pattern cannot be set.
无法设置日期模式。 Does not compress rotated files.
不压缩旋转的文件。
Solution for First issue, is described here 这里描述第一个问题的解决方案
Solution for Second issue is described here 这里描述了第二个问题的解决方案
Solution for Third issue is described here 此处描述了第三个问题的解决方案
You can as well combine solutions. 您也可以组合解决方案。 For example use crontab to gzip files that where created by changing catalina.out to other name.
例如,使用crontab通过将catalina.out更改为其他名称来创建gzip文件。 I would also suggest to leave tomcat so it logs to catalina.out, and configure your application to different file with log4j.
我还建议离开tomcat,以便记录到catalina.out,并使用log4j将应用程序配置到不同的文件。 This way logs from tomcat that are not immaterial won't spam your logs.
这种方式来自tomcat的非易失性日志不会垃圾邮件。
Is it even possible to rotate my logfiles without restarting Tomcat?
甚至可以在不重启Tomcat的情况下旋转我的日志文件吗?
Yes, if you're willing to work for it. 是的,如果你愿意为它工作。
Your log4j configuration will only end up fighting with the standard shell redirection that bin/catalina.sh uses to redirect stdout to logs/catalina.out. 您的log4j配置最终将与bin / catalina.sh用于将stdout重定向到logs / catalina.out的标准shell重定向进行斗争。 You can't simply use log4j configuration to change how System.out behaves.
您不能简单地使用log4j配置来更改System.out的行为方式。
If you want to rotate conf/catalina.out you will have to take some alternative measures depending on how to launch Tomcat: 如果你想旋转conf / catalina.out,你将不得不采取一些替代措施,具体取决于如何启动Tomcat:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.