log4j.xml实例

tomcat/webapps/myProject/WEB-INF/log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%p] %d [%c.%M] - %m%n"/>
        </layout>
    </appender>
    
    <appender name="file" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="${appRoot}logs/run.log" />
        <param name="Threshold" value="DEBUG" />
        <param name="Append" value="true" />
        <param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%p] %d [%c.%M] - %m%n"/>
        </layout>
    </appender>
    
    <appender name="db" class="com.myProject.util.log.ExtendAppender"> 
        <!--  <layout class="org.apache.log4j.PatternLayout"> 
        	<param name="ConversionPattern" 
        		value="insert into OPERLOG (id, logdate, loglevel, module, function, message) values (seq_OPERLOG.nextval, \'%d\',\'%p\', \'%c{1}\', \'%M\', \'%m\')" /> 
        </layout> -->
    </appender> 

    <logger name="org.apache" additivity="false">
        <level value="WARN"/>
        <appender-ref ref="file"/>
    </logger>

    <logger name="org.zkoss" additivity="false">
        <level value="WARN"/>
        <appender-ref ref="file"/>
    </logger>
    
    <logger name="org.springframework">
        <level value="WARN"/>
    </logger>
    
    <logger name="java.sql">
        <level value="INFO"/>
    </logger>
    
    <logger name="com.myProject">
        <level value="INFO"/>
    </logger>
    
    <root>
        <level value="ERROR"/>
        <appender-ref ref="console"/>
        <appender-ref ref="file"/>
        <appender-ref ref="db"/>
    </root>
</log4j:configuration>

其中, ${appRoot}是指应用的根目录tomcat/webapps/myProject/,它配置在web.xml文件中:
	<context-param>
		<param-name>webAppRootKey</param-name>
		<param-value>appRoot</param-value>
	</context-param>
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>/WEB-INF/log4j.xml</param-value>
	</context-param>
	
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>

你可能感兴趣的:(java,spring,tomcat,Web,log4j)