开发者

Spring 3 SimpleMappingExceptionResolver warnLogCategory log4j

<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
            <map>
              <entry key="j开发者_StackOverflowava.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


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜