Log4j每天滚动catalina.out而不重启Tomcat? [英] Log4j daily rolling catalina.out without restarting Tomcat?

查看:354
本文介绍了Log4j每天滚动catalina.out而不重启Tomcat?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法正确配置Log4j。我希望Log4j在午夜时调整我的catalina.out文件,如下所示进行配置..




log4j.properties:

i am having trouble with configuring Log4j correctly. I was expecting Log4j to rotate my catalina.out file at midnight when configuring it like the following..


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


配置后我重新启动Tomcat和
所有内容都写入:

After configuring I restarted Tomcat and everything is written to:

/var/log/tomcat7/catalina.out



测试我的confi guration我将当前日期时间更改为23:59:59:


To test my configuration i changed the current date time to like 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... (?)

当重启Tomcat时它工作得很好:

When restarting Tomcat it works perfectly fine:

#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

是否可以在不重启Tomcat的情况下旋转我的日志文件?

Is it even possible to rotate my logfiles without restarting Tomcat?

提前致谢,
Marley

Thanks in advance, Marley

推荐答案

这个问题有三个解决方案:

There are three solutions for this problem:


  1. 更改写入catalina.out的默认tomcat日志外观,例如:slf4j,具有所有好处使用它和log4j。

  2. 配置系统cron以运行tomcat日志文件的logrotate

  3. 将默认日志记录类从ConsoleAppender更改为FileAppender。 / li>
  1. change default tomcat logging façade that writes to catalina.out to for example: slf4j, with all the benefits that comes with using it and log4j.
  2. configure system cron to run logrotate of tomcat log files
  3. change default logging class from ConsoleAppender to FileAppender.

解决方案的好处:


  1. 非常灵活因为slf4j提供了许多选项,尤其是你使用的log4j。

  2. 简单而且不需要触摸tomcat配置。

  3. 简单的改变禁用控制台输出的配置

缺点:


  1. 需要额外的库,影响tomcat托管的所有应用程序,需要用lo替换默认配置g4j。

  2. cron + logrotate仅适用于linux;在带调度程序的Windows中可能不那么简单。在Windows环境中需要额外的脚本。

  3. 仅提供带日期的简单备份。无法设置日期模式。不压缩旋转的文件。

  1. require additional libraries, affects all applications that tomcat is hosting, requires replacing default configuration with log4j.
  2. cron+logrotate works only in linux; it might be not as simple in windows with scheduler. Requires extra scripting in windows environment.
  3. 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

您也可以组合解决方案。例如,使用crontab通过将catalina.out更改为其他名称来创建gzip文件。
我还建议离开tomcat,使其登录到catalina.out,并使用log4j将应用程序配置到不同的文件。这种来自tomcat的非易失性日志不会垃圾邮件。

You can as well combine solutions. For example use crontab to gzip files that where created by changing catalina.out to other name. I would also suggest to leave tomcat so it logs to catalina.out, and configure your application to different file with log4j. This way logs from tomcat that are not immaterial won't spam your logs.

这篇关于Log4j每天滚动catalina.out而不重启Tomcat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆