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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8079915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:26:32  来源:igfitidea点击:

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

windowstomcatrotationlogging

提问by Raghu GS

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. cronologdidn't work, no files where being created after setting it up.

  2. Managed to configure log4jwith DailyRollingFileappender globally for Tomcat, the log files are being created without extension and are not being rotated.

  3. Tried DatedFileAppendertoo, but didn't get much success.

  4. Tried log4jnabased Windows Event Log appenderfor log4j, there didn't get success

  1. cronolog没有用,设置后没有创建文件。

  2. 管理配置log4jDailyRollingFile附加器在全球范围为Tomcat,被无扩展创建的日志文件,并且不旋转。

  3. 也尝试DatedFileAppender过,但没有取得太大的成功。

  4. 尝试log4jna基于Windows Event Log appenderlog4j,没有成功

Please suggest an easy method to finish this task.

请提出一个简单的方法来完成这个任务。

回答by Costis Aivalis

Apache Tomcat provides the configuration directory conf, that contains the file server.xml. At the bottom of this file, a line that includes the adjustments valve, called AccessLogValvemust be un-commented and changed.

Apache Tomcat 提供配置目录conf,其中包含文件server.xml。在此文件的底部,必须取消注释并更改包含调整阀的一行,称为 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 fileDateFormatparameter allows a customized date format in the access log file name. 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

fileDateFormat参数允许在访问日志文件名的自定义日期格式。日期格式还决定了文件的轮换频率。如果您希望每小时轮换一次,则将此值设置为: yyyy-MM-dd.HH

回答by Raghu GS

I have finally resolved the problem after a long stretch. This time, i have not touched tomcat log configuration. I used a fantastic utility called logrotatewin which is an reimplementation of logrotate in Windows. I was already familiar with logrotate, so setting up logrotatewin was an easy job. 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日志配置。我使用了一个名为 logrotatewin 的奇妙实用程序,它是 Windows 中 logrotate 的重新实现。我已经熟悉 logrotate,所以设置 logrotatewin 是一件容易的事。我不得不在预运行中停止 tomcat 并在运行后重新启动它以使日志轮换工作,因为 tomcat 在运行时会锁定日志文件。

回答by adityalad

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.

我一直在寻找如何轮换 Access Valve 日志。从我到目前为止收集的任何数据来看,“旋转”只会根据您设置的 fileDateFormat 值切换到下一个文件。

For e.g. a value of "yyyy-MM-dd.HH" would create a new file every hour. 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.

例如,“yyyy-MM-dd.HH”的值将每小时创建一个新文件。但似乎文件的数量会继续增长,除非您使用像 cron 作业(在 linux 中)这样的外部机制来清理它们。