简体   繁体   English

如何在Windows中旋转tomcat日志? 最好的方法是什么?

[英]How to rotate tomcat logs in Windows? What is the best method?

I am really fed up in not being able to solve the tomcat log rotation problem in one our windows server after putting countless hours in it and after trying out various solutions. 在投入无数小时并尝试各种解决方案后,我真的无法解决我们的Windows服务器中的tomcat日志轮换问题。

  • Environment: Java 1.6x, Tomcat 6 (running as service), log4j 1.2, Server 2003 32-bit 环境: Java 1.6x,Tomcat 6(作为服务运行),log4j 1.2,Server 2003 32位

Methods tried so far: 到目前为止尝试过的方法:

  1. cronolog didn't work, no files where being created after setting it up. cronolog不起作用,设置后没有文件创建。

  2. Managed to configure log4j with DailyRollingFile appender globally for Tomcat, the log files are being created without extension and are not being rotated. 通过使用全局的Tomcat的DailyRollingFile附加程序对log4j进行配置,创建的日志文件没有扩展名,并且不会旋转。

  3. Tried DatedFileAppender too, but didn't get much success. 也尝试过DatedFileAppender ,但是没有获得太大的成功。

  4. Tried log4jna based Windows Event Log appender for log4j, there didn't get success 尝试对log4j使用基于log4jnaWindows Event Log appender ,但未成功

Please suggest an easy method to finish this task. 请提出一种简单的方法来完成此任务。

Apache Tomcat provides the configuration directory conf , that contains the file server.xml . Apache Tomcat提供了配置目录conf ,其中包含文件server.xml At the bottom of this file, a line that includes the adjustments valve, called AccessLogValve must be un-commented and changed. 在此文件的底部,必须取消注释和更改包含调整阀的行,称为AccessLogValve

The default valve parameters and patterns can easily be modified through an editor. 阀门的默认参数和模式可以通过编辑器轻松修改。 Your settings for the access log file for example may look like this: 例如,您对访问日志文件的设置可能如下所示:

<Valve
className="org.apache.catalina.valves.AccessLogValve."
directory="logs"
prefix="mysite."
suffix=".log"
pattern='%a %A %b %B %h %l %m %p %q %u %t "%r" %s %U %D %S'
resolveHosts="false"
rotatable="true"
fileDateFormat="yyyy-MM-dd"
/>

This sentence is from the Tomcat reference : 这句话来自Tomcat参考

The fileDateFormat parameter allows a customized date format in the access log file name. fileDateFormat参数允许在访问日志文件名中使用自定义的日期格式。 The date format also decides how often the file is rotated. 日期格式还可以决定文件轮换的频率。 If you wish to rotate every hour, then set this value to: yyyy-MM-dd.HH 如果希望每小时旋转一次,请将此值设置为: yyyy-MM-dd.HH

I have finally resolved the problem after a long stretch. 经过漫长的努力,我终于解决了这个问题。 This time, i have not touched tomcat log configuration. 这次,我还没有碰到tomcat日志配置。 I used a fantastic utility called logrotatewin which is an reimplementation of logrotate in Windows. 我使用了一个很棒的实用程序logrotatewin,它是Windows中logrotate的重新实现。 I was already familiar with logrotate, so setting up logrotatewin was an easy job. 我已经很熟悉logrotate,因此设置logrotatewin很容易。 I had to stop tomcat in prerun and start it back in postrun for the log rotation to work, as tomcat locks the log file when it is running. 我必须在运行前停止tomcat,然后在运行后重新启动tomcat,以使日志轮换正常工作,因为tomcat在运行时会锁定日志文件。

I have been searching on how to rotate the Access Valve logs. 我一直在寻找如何旋转检修阀日志。 From what ever data I have gathered so far, the 'rotation' would simply switch over to the next file based on the fileDateFormat value that you set. 从到目前为止我收集的数据来看,“轮换”将根据您设置的fileDateFormat值简单地切换到下一个文件。

For eg a value of "yyyy-MM-dd.HH" would create a new file every hour. 例如,值“ yyyy-MM-dd.HH”将每小时创建一个新文件。 But it seems the files would continue growing in number unless you use an external mechanism like a cron job (in linux) to clean them up. 但是,除非您使用诸如cron job(在Linux中)之类的外部机制来清理它们,否则文件的数量似乎将继续增长。

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

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