Spring 3 SimpleMappingExceptionResolver warnLogCategory log4j

China☆狼群 提交于 2020-01-03 07:21:37

问题


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

I would like to log the exception above into a .log file, but it does not log =(. Could someone comment on what might be wrong with my log4j properties...or anything else?

Using Spring 3.0.5

thanks

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

回答1:


my solution is override method logException in SimpleMappingExceptionResolver

new resolver:

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>

I think logback config is similar with log4j, hope could help you




回答2:


Try to extend it with something like:

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>

But this is basically the same thing as:

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

with the correct log4j.logger.abcdefg=WARN




回答3:


You have multiple log4j.jar provided to you application by multiple classloaders and your app is finding the wrong one and using the default configuration from there almost certainly. You can verify this by starting your application with

java -Dlog4j.configuration=/path/to/log4j.properties ... StartupClass

log4j will always use the system property before it searches for another file.

If that fixes your problem you will need to tweak your build to only include the log4j related resources that you really need and remove the system property.



来源:https://stackoverflow.com/questions/4626728/spring-3-simplemappingexceptionresolver-warnlogcategory-log4j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!