Nikhil Agrawal
Nikhil Agrawal

Reputation: 21

How to rotate Tomcat Access Log file, by file size rather than by the date

Presently my access log file (access_log.yyyy-MM-dd.log) is generating everyday(big size file) and keep on a accumulating day by day. I want's to rotate Access Log files by file size.

I tried by :

<Valve className="org.apache.catalina.valves.AccessLogValve"
         directory="${catalina.home}/../logs"  prefix="access_log." suffix=".txt"
         rotatable ="false"
         pattern="common" resolveHosts="false"/>

But its not working for size,its generating only a single file and add's logs from all date in same file . How to made it rotate by size of log file?

Upvotes: 1

Views: 7575

Answers (1)

Beck Yang
Beck Yang

Reputation: 3024

By default, Tomcat does't does not support rotating files by size, you can modify $TOMCAT_HOME/conf/logging.properties by yourself. Please refer How To Configure Apache Tomcat 6.0/7.0 or tc Server to rotate log files by size with logging.properties or other articles.

Update in 2017/8/30

The solution is implement a Tomcat Valve element. Here is the sample code that write access log using java.util.logging API:

package org.mylog;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.util.logging.*;

public class CustomAccessLog extends org.apache.catalina.valves.AbstractAccessLogValve {
    public static class NoFormat extends Formatter {
        public synchronized String format(LogRecord record) {
            return record.getMessage();
        }   
    }

    private Logger log = Logger.getLogger("access_log");
    public CustomAccessLog() {
        log = Logger.getLogger("access_log");
        //not required, use the formatter that only print log message.
        Handler[] ha = log.getHandlers();
        for (int i = 0; i < ha.length; i++) {
            ha[i].setFormatter(new NoFormat());
        }
    }
    //@Override
    public void log(CharArrayWriter message) {
        log.info(message.append('\n').toString());
    }
}

Packaing compiled class to jar and copy it to %TOMCAT_HOME/lib

Modify %TOMCAT_HOME/conf/server.xml, replace

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" ... />

with following one

<Valve className="org.mylog.CustomAccessLog" pattern="%h %l %u %t &quot;%r&quot; %s %b" />

Modify %TOMCAT_HOME/conf/logging.properties add log config for CustomAccessLog.

#Remember to add 9access.java.util.logging.FileHandler to "handlers"
#for example:
#handlers = 1catalina.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler, 9access.java.util.logging.FileHandler
9access.java.util.logging.FileHandler.level = FINE
9access.java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
9access.java.util.logging.FileHandler.pattern = ${catalina.base}/logs/access_log.%g.txt
9access.java.util.logging.FileHandler.limit = 1000000
9access.java.util.logging.FileHandler.count = 5
access_log.level = FINE
access_log.handlers = 9access.java.util.logging.FileHandler

I test it in Tomcat v8.5.20, but it also work on 6.x/7.x/8.x

Upvotes: 3

Related Questions