2013-08-05 175 views
3

我有一个干净的Tomcat7安装启用log4j(根据http://tomcat.apache.org/tomcat-7.0-doc/logging.html#Using_Log4j)。 “应用程序级”日志记录使用log4j配置(我将日志定向到本地系统日志服务器)。如何使我的应用程序access.log也使用log4j?据我所见,我可以在AccessLogValve,ExtendedAccessLogValve和JDBCAccessLogValve中进行选择,但更重要的是,它们使我能够登录syslog并使用log4j。Tomcat通过log4j访问日志记录?

回答

3

log4j-scribe-appender项目包含一个Log4JAccessLogValve,它可以让你做到这一点。

+0

经过我的测试,log4j-scribe-appender只能用于Tomcat 6.需要用Tomcat 7重写Log4JAccessLogValve。 – Ben

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(); 
      } 
     } 
    } 

} 
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> 

只需编译,制作一个罐子,在/usr/share/tomcat7/lib/中放置一个副本,并修改server.xml。我将它添加到我在github上的reference tomcat7 configuration