我有一个干净的tomcat7安装,启用了log4j(根据http://tomcat.apache.org/tomcat-7.0-doc/logging.html#Using_Log4j)。 “应用程序级”日志记录确实使用log4j配置(我将日志指向本地syslog服务器)。如何使我的应用程序 access.log 也使用log4j?据我所知,我可以在AccessLogValve,ExtendedAccessLogValve和JDBCAccessLogValve中进行选择,但是更新的它们使我能够登录到syslog,也无法使用log4j。
答案 0 :(得分:3)
log4j-scribe-appender项目包含Log4JAccessLogValve,可让您这样做。
答案 1 :(得分:0)
如果您使用Tomcat 7,则可以通过覆盖此函数来扩展AccessLogValve:
/**
* Log the specified message to the log file, switching files if the date
* has changed since the previous log call.
*
* @param message Message to be logged
*/
public void log(String message) {
rotate();
/* In case something external rotated the file instead */
if (checkExists) {
synchronized (this) {
if (currentLogFile != null && !currentLogFile.exists()) {
try {
close(false);
} catch (Throwable e) {
ExceptionUtils.handleThrowable(e);
log.info(sm.getString("accessLogValve.closeFail"), e);
}
/* Make sure date is correct */
dateStamp = fileDateFormatter.format(
new Date(System.currentTimeMillis()));
open();
}
}
}
// Log this message
synchronized(this) {
if (writer != null) {
writer.println(message);
if (!buffered) {
writer.flush();
}
}
}
}
答案 2 :(得分:0)
这适用于JDK日志记录:
package org.apache.plugins;
import java.util.logging.Logger;
import org.apache.catalina.valves.AccessLogValve;
public class AccessLogJdkValve extends AccessLogValve {
private static Logger jdkLogger = Logger.getLogger(AccessLogJdkValve.class.getName());
@Override
public void log(String msg) {
jdkLogger.info(msg);
}
@Override
protected synchronized void open() {
// do nothing
}
}
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tomcat7-conf</groupId>
<artifactId>tomcat7-conf</artifactId>
<version>1.0-RELEASE</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>common-tomcat-maven-plugin</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>7.0.12</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
只需编译,制作一个jar,在/usr/share/tomcat7/lib/
中复制一份,然后修改server.xml
。我将它添加到github上的reference tomcat7 configuration。