Spring 3 SimpleMappingExceptionResolver warnLogCategory log4j

时间:2011-01-07 14:53:30

标签: java spring spring-mvc java-ee

<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
            <map>
              <entry key="java.lang.Exception" value="error"/>
            </map>
          </property>
<property name="warnLogCategory" value="abcdefg"/>
</bean>

我想将上面的异常记录到.log文件中,但它没有记录=(。有人可以评论我的log4j属性可能有什么问题......还是其他什么?

使用Spring 3.0.5

感谢

log4j.rootLogger=DEBUG, stdout, pqe
log4j.category.abcdefg=WARN, pqe

log4j.appender.pqe=org.apache.log4j.DailyRollingFileAppender
log4j.appender.pqe.DatePattern=_yyyyMMdd
log4j.appender.pqe.File=D:\\pqe.log
log4j.appender.pqe.layout=org.apache.log4j.PatternLayout
log4j.appender.pqe.layout.ConversionPattern=%d|%5p|%c %m%n

3 个答案:

答案 0 :(得分:3)

我的解决方案是 SimpleMappingExceptionResolver

中的覆盖方法 logException

新的解析器:

public class LoggingExceptionResolver extends SimpleMappingExceptionResolver {
private Logger logger = LoggerFactory.getLogger(LoggingExceptionResolver.class);

@Override
protected void logException(Exception ex, HttpServletRequest request) {
    this.logger.warn(buildLogMessage(ex, request), ex);
}

}

spring config:

<bean id="exceptionResolver" class="com.zyam.isu.core.utils.log.LoggingExceptionResolver">
    <property name="defaultErrorView">
        <value>error.jsp</value>
    </property>
    <property name="exceptionMappings">
        <props>
            <prop key="java.lang.RuntimeException">error.jsp</prop>
            <prop key="java.lang.Exception">error.jsp</prop>
        </props>
    </property>
</bean>

logback.xml

    <logger name="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <level value="warn" />
</logger>

我认为logback配置与log4j类似,希望能帮到你

答案 1 :(得分:2)

尝试使用以下内容扩展它:

public class LoggingExceptionResolver extends SimpleMappingExceptionResolver {

     public LoggingExceptionResolver(String category) {
         super();
         this.warnLogger = LoggerFactory.getLogger(category); // or whatever log implementation
     }

}

<bean id="exceptionResolver" class="package.name.LoggingExceptionResolver">
    <constructor-arg value="abcdefg"/>
    <property name="exceptionMappings">
        <map>
            <entry key="java.lang.Exception" value="error"/>
        </map>
    </property>
</bean>

但这与基本相同:

<!-- whatever the implmentation/class name is -->
<bean id="log" class="org.apache.log4j.logger.Logger" factory-method="getLogger">
    <constructor-arg value="abcdefg"/>
</bean>

<bean id="exceptionResolver" class="package.name.LoggingExceptionResolver">
    <property name="warnLogger" ref="log" />
    <property name="exceptionMappings">
        <map>
            <entry key="java.lang.Exception" value="error"/>
        </map>
    </property>
</bean>

使用正确的log4j.logger.abcdefg=WARN

答案 2 :(得分:0)

您有多个类加载器为您的应用程序提供了多个log4j.jar,您的应用程序找到了错误的并且几乎可以肯定地使用默认配置。您可以通过

启动应用程序来验证这一点
java -Dlog4j.configuration=/path/to/log4j.properties ... StartupClass

log4j在搜索另一个文件之前将始终使用系统属性。

如果这可以解决您的问题,则需要调整您的构建,以便仅包含您真正需要的log4j相关资源并删除系统属性。