How to rotate the tomcat localhost log?
Asked Answered
M

4

17

I am using tomcat 6x in a Linux system. It prints a localhost log file like localhost.2011-06-07, localhost.2011-06-08 on a daily basis. I want to rotate the localhost when it reaches 1MB.

I can rotate log files in log4j for my web apps. But this localhost log file of tomcat, I couldn't get it to rotate. Has any got a solution other than using logrotate?

Mismate answered 8/6, 2011 at 12:22 Comment(1)
I am very curious about this as well... have you found out anything?Ferneferneau
P
21

Do you not want to use logrotate, or does your sys admin not allow you to?

Given that restriction, I'm afraid (from what I have been able to learn from the documentation) that there is no automatic way to do what you want (rotate log files based on the size alone).

If you merely want to stop Tomcat's default behavior of creating a new log every day (which I find extremely annoying for a development environment or low-traffic site), you can certainly do this by changing the fileDateFormat property of the Access Log "Valve", which on Ubuntu Server is defined in /etc/tomcat7/server.xml. It's probably in a similar location for Tomcat 6.

Since you mention a restrictive sys admin, I gather that this server is not under your control, so that path is irrelevant. If you do have the ability to modify Tomcat's log configuration, hopefully you know where to find the appropriate file.

In that .xml file, look for the Valve entity with className set to "org.apache.catalina.valves.AccessLogValve" in the section which configures the "Catalina" Engine for the appropriate host for your site (localhost in my case). Although the Tomcat 6 documentation doesn't mention it, one can deduce that the default fileDateFormat is "yyyy-MM-dd". Including the day in the log filename tells Tomcat (by implication) that you also want the log rotated every day. If you wanted it to be rotated, say, only once a month, just change the fileDateFormat to "yyyy-MM".

On my server, the default Logging Valve definition was this:

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="localhost_access_log." suffix=".txt"
           pattern="%h %l %u %t &quot;%r&quot; %s %b" />

To change this to monthly log rotation, I would just add the appropriate fileDateFormat attribute:

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           fileDateFormat="yyyy-MM" prefix="localhost_access_log." suffix=".txt"
           pattern="%h %l %u %t &quot;%r&quot; %s %b" />

Since I am able (and willing) to use logrotate, I have turned off Tomcat's built-in log rotation and date-based file naming completely, and simply use logrotate to rotate the logs whenever the log reaches 1MB.

Edit To answer dgrant's question, the changes were to /etc/tomcat7/server.xml, and the actual valve configuration I'm using is this:

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
       pattern="combined" rotatable="false"
       prefix="access_log" />

Note that the 'combined' pattern is equivalent (at least for Tomcat 7) to the explicit pattern definition in the original configuration. All this is well-covered in my original documentation link if you want to read more. Just look for the section on the rotatable attribute.

Prodigal answered 31/7, 2012 at 18:54 Comment(3)
"I have turned off Tomcat's built-in log rotation and date-based file naming completely" - how exactly did you do that?Oldham
found it in here. just set rotatable ="false" so in conf/server.xml you will have entry like <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" rotatable ="false" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access" suffix=".log"/>Endorsed
See also https://mcmap.net/q/392712/-how-to-rotate-the-tomcat-localhost-log - the renameOnRotate attribute will be useful to some folks.Prodigal
F
7

Just so you know, I ended up replacing the default Tomcat logging (java.util.logging) with Log4J following the instructions on the Tomcat website: http://tomcat.apache.org/tomcat-6.0-doc/logging.html#Using_Log4j

Ferneferneau answered 12/7, 2011 at 18:42 Comment(2)
Collin: and did this actually fix the problem, that access logs are rotated by file size? As far as I understand it, Tomcat would still use its AccessLogValve, which has its completely own rules regarding filename patterns and rotation. Totally independent from the JUL or log4j config.Yl
I'm not sure about the access log. I have that turned off as we have Apache in front of Tomcat. The log4j stuff does work for catalina.out and localhost.log, etc..Ferneferneau
A
3

This should be easy on Tomcat 7: see this docs

renameOnRotate attribute

By default for a rotatable log the active access log file name will contain the current timestamp in fileDateFormat. During rotation the file is closed and a new file with the next timestamp in the name is created and used. When setting renameOnRotate to true, the timestamp is no longer part of the active log file name. Only during rotation the file is closed and then renamed to include the timestamp. This is similar to the behavior of most log frameworks when doing time based rotation. Default value: false

Arredondo answered 20/4, 2015 at 13:32 Comment(4)
Very useful to know, thanks. I up-voted it a while back but imagine it got down-voted because it's not a complete answer. A sample <Valve> configuration using this attribute would probably be more helpful than just a link to a big documentation page.Prodigal
@Prodigal I don't get used to provide fishes, I'm glad to help other people fish. You only have to open up the documentation page and read about renameOnRotate attribute to use it. Not a big deal.Arredondo
I hear you, and fishing lessons are great, but there are lots of tasty fish in the pond here at StackOverflow, too... and it seems they usually get the most up-votes. :)Prodigal
I downvoted this answer, because it doesn't seem to answer the question. The question asked how to get the log to rotate based on size, which doesn't seem to have anything to do with the renameOnRotate attribute.Revest
V
1

I am using mmlog which is a simple, lightweight, and not-intrusive tool. Please refer to "https://github.com/liurui-1/mmlog". I have used it in production systems for many years.

For example your Tomcat installed on Linux/x64 and is located in directory "TOMCAT_HOME=/opt/apache-tomcat-9.0.44". Down mmlog for Linux from page: https://github.com/liurui-1/mmlog/blob/master/build/linux.amd64/mmlog and copy to directory "$TOMCAT_HOME/bin". Use following command to start Tomcat. Then you make your Tomcat log rotated. More details please refer to "https://github.com/liurui-1/mmlog".

export TOMCAT_HOME=/opt/apache-tomcat-9.0.44
export CATALINA_OUT_CMD="nohup $TOMCAT_HOME/bin/mmlog"
export MM_LOGFILE=$TOMCAT_HOME/logs/tomcat
export MM_LOGSIZE=1000000
$TOMCAT_HOME/bin/startup.sh
Villarreal answered 17/5, 2021 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.