简体   繁体   English

Log4j每天滚动catalina.out而不重启Tomcat?

[英]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: 这个问题有三种解决方案:

  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. 将写入catalina.out的默认tomcat日志外观更改为例如:slf4j,具有使用它和log4j带来的所有好处。
  2. configure system cron to run logrotate of tomcat log files 配置系统cron以运行tomcat日志文件的logrotate
  3. change default logging class from ConsoleAppender to FileAppender. 将默认日志记录类从ConsoleAppender更改为FileAppender。

Benefits of solutions: 解决方案的好处:

  1. very flexible as the slf4j offers many options especially with log4j, that you use anyway. 非常灵活,因为slf4j提供了许多选项,尤其是log4j,无论如何都要使用。
  2. simple and doesn't require touching tomcat configuration. 简单而且不需要触摸tomcat配置。
  3. simple change of configuration that disables console output 简单的配置更改,禁用控制台输出

Disadvantages: 缺点:

  1. require additional libraries, affects all applications that tomcat is hosting, requires replacing default configuration with log4j. 需要额外的库,影响tomcat托管的所有应用程序,需要用log4j替换默认配置。
  2. cron+logrotate works only in linux; cron + logrotate仅适用于linux; it might be not as simple in windows with scheduler. 在带调度程序的Windows中可能不那么简单。 Requires extra scripting in windows environment. 在Windows环境中需要额外的脚本。
  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 此处描述了第三个问题的解决方案

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:

  • If you use jsvc to launch Tomcat and you are using commons-daemon 1.0.4 or later, then you can send SIGUSR1 to the jsvc process to re-open the log files. 如果使用jsvc启动Tomcat并使用commons-daemon 1.0.4或更高版本,则可以将SIGUSR1发送到jsvc进程以重新打开日志文件。 That will allow you to move the existing log file to another file (which just changes its name and continues to log to the new filename) and then do 'kill SIGUSR1': the original filename will then be re-opened and new logs messages will go to it. 这将允许您将现有日志文件移动到另一个文件(只更改其名称并继续记录到新文件名)然后执行'kill SIGUSR1':然后将重新打开原始文件名,并且新的日志消息将去吧。
  • If you use bin/catalina.sh to launch Tomcat, you can modify it so that it no longer does redirection and instead pipes output to a rolling-logger process like Apache httpd's rotatelogs or chronolog. 如果使用bin / catalina.sh来启动Tomcat,则可以对其进行修改,使其不再进行重定向,而是将输出管道输出到滚动记录器进程,如Apache httpd的rotatelogs或chronolog。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM