java Spring 3 SimpleMappingExceptionResolver warnLogCategory log4j

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4626728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:16:42  来源:igfitidea点击:

Spring 3 SimpleMappingExceptionResolver warnLogCategory log4j

javaspringspring-mvcjakarta-ee

提问by john

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

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

Using Spring 3.0.5

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

回答by Jerry Sky Walker

my solution is override method logExceptionin SimpleMappingExceptionResolver

我的解决办法是重写方法logException的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

我认为 logback 配置与 log4j 类似,希望可以帮助您

回答by hisdrewness

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

用正确的 log4j.logger.abcdefg=WARN

回答by sw1nn

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

您有多个由多个类加载器提供给您的应用程序的 log4j.jar,并且您的应用程序正在查找错误的一个,并且几乎可以肯定地使用那里的默认配置。您可以通过启动您的应用程序来验证这一点

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

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

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

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.

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