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

17,568

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.

Share:
17,568
Raghu GS
Author by

Raghu GS

Updated on June 15, 2022

Comments

  • Raghu GS
    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:

    1. cronolog didn't work, no files where being created after setting it up.

    2. Managed to configure log4j with DailyRollingFile appender globally for Tomcat, the log files are being created without extension and are not being rotated.

    3. Tried DatedFileAppender too, but didn't get much success.

    4. Tried log4jna based Windows Event Log appender for log4j, there didn't get success

    Please suggest an easy method to finish this task.

  • Raghu GS
    Raghu GS over 12 years
    Thanks 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
    Raghu GS over 12 years
    Still 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