Tomcat原生日志长期运行可能导致硬盘被耗尽,影响系统运行,本文讨论对Tomcat日志的绕接方案,使用log4j三方件的能力接管Tomcat日志能力从而实现绕接。
把需要jar包复制到tomcat lib
主要涉及下面三个包:
log4j-api-2.3.jar log4j-core-2.3.jar log4j-jul-2.3.jar
设置环境变量
编辑setenv.sh
#!/bin/sh
export "CLASSPATH=$CLASSPATH:$CATALINA_HOME/lib/log4j-jul-2.3.jar:$CATALINA_HOME/lib/log4j-api-2.3.jar:$CATALINA_HOME/lib/log4j-core-2.3.jar"
export
CATALINA_OPTS="$CATALINA_OPTS
-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
-Dlog4j.configurationFile=$CATALINA_HOME/conf/log4j2.xml"
编辑log4j2.xml
<?xml version="1.0" encoding="utf-8"?>
<Configuration status="info">
<Properties>
<Property name="logdir">${sys:catalina.base}/logs</Property>
<Property name="layout">%d [%t] %-5p %c-%m%n</Property>
</Properties>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="${layout}"/>
</Console>
<RollingFile name="CATALINA" fileName="${logdir}/catalina.log"
filePattern="${logdir}/catalina.%d{yyyy-MM-dd}.log.gz">
<PatternLayout pattern="${layout}"/>
<SizeBasedTriggeringPolicy size="50MB"/>
<!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了20-->
<DefaultRolloverStrategy max="20"/>
</RollingFile>
<RollingFile name="LOCALHOST" fileName="${logdir}/localhost.log" filePattern="${logdir}/localhost.%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout pattern="${layout}"/>
<SizeBasedTriggeringPolicy size="50MB"/>
<!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了20-->
<DefaultRolloverStrategy max="20"/>
</RollingFile>
<RollingFile name="ACCESSLOG" fileName="${logdir}/localhost_access.log"
filePattern="${logdir}/localhost_acess.%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout pattern="${layout}"/>
<SizeBasedTriggeringPolicy size="50MB"/>
<!-- DefaultRolloverStrategy属性如不设置,则默认为最多同一文件夹下7个文件,这里设置了20 -->
<DefaultRolloverStrategy max="20"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.huawei.cloudsop.tomcat.api.log" level="debug" additivity="false">
<AppenderRef ref="ACCESSLOG"/>
</Logger>
<Logger name="org.apache.catalina.core.ContainerBase" level="info"
additivity="false">
<AppenderRef ref="LOCALHOST"/>
</Logger>
<Root level="info">
<AppenderRef ref="CATALINA"/>
</Root>
</Loggers>
</Configuration>
修改server.xml
<Valve className="com.xxx.tomcat.api.log.AccessLogValveExt"
pattern="%h %l %u %t "%r" %s %b %{x-request-id}i"/>
编写绕接类AccessLogValveExt
package com.xxx.tomcat.api.log;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.AccessLogValve;
import java.io.CharArrayWriter;
public class AccessLogValveExt extends AccessLogValve
{
*/**
* The descriptive information about
this implementation.
*/
*protected static final String *info *=
"com.huawei.cloudsop.tomcat.api.log.AccessLogValveExt/1.0";
private static final org.apache.juli.logging.Log accessLog =
org.apache.juli.logging.LogFactory.getLog(AccessLogValveExt.class);
// rewrite log func of
public void log(CharArrayWriter message) {
accessLog.info(message.toString());
}
@Override
public void log(Request request, Response response, long time) {
super.log(request, response, time);
}
public String getInfo() {
return info;
}
@Override
protected synchronized void open() {
// do nothing
}
}
大功告成