How to rotate tomcat logs in Windows? What is the best method?
Solution 1
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 AccessLogValve must be un-commented and changed.
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:
The fileDateFormat parameter 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
Solution 2
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.
Raghu GS
Updated on June 15, 2022Comments
-
Raghu GS almost 2 years
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.
- Environment: Java 1.6x, Tomcat 6 (running as service), log4j 1.2, Server 2003 32-bit
Methods tried so far:
cronolog
didn't work, no files where being created after setting it up.Managed to configure
log4j
withDailyRollingFile
appender globally for Tomcat, the log files are being created without extension and are not being rotated.Tried
DatedFileAppender
too, but didn't get much success.Tried
log4jna
basedWindows Event Log appender
for log4j, there didn't get success
Please suggest an easy method to finish this task.
-
Raghu GS over 12 yearsThanks for taking your time to write to me such detailed reply. You are approaching the problem from totally a different angle. I am not sure whether this would solve my problem or not. But as of now i have managed to configure the logs rotation via dailyrollingfile appender in log4j
-
Raghu GS over 12 yearsStill logs are not being rotated even after successfully configuring the dailyrollingfile appender. I think log4j can't rename a file when being used by Tomcat in Windows. Please suggest me other options