在我的struts 2动作类中发生错误时,Log4j没有记录

时间:2014-03-21 11:04:29

标签: logging struts2

在我的Struts 2应用程序中,发生错误时没有堆栈跟踪。为了在我的动作类中测试它,我创建了一个空对象,我尝试在该对象上调用一个方法。 Struts 2过滤器向我显示错误屏幕,因此excepions映射是正确的,但在日志文件中我什么都没有。得到一些东西的唯一方法是在catch语句中添加带有log.error(e)的try / catch。

struts.xml:

<interceptors>
            <interceptor name="authz" class="com.omb.controller.security.AuthzInterceptor"/>

            <interceptor name="params-filter" class="com.opensymphony.xwork2.interceptor.ParameterFilterInterceptor"/>
            <interceptor name="user" class="com.omb.controller.interceptor.UserInterceptor"/>

            <interceptor-stack name="defaultStack">
                <interceptor-ref name="exception">
                    <param name="exception.logEnabled">true</param>
                    <param name="exception.logLevel">ERROR</param>
                </interceptor-ref>
                <interceptor-ref name="alias"/>
                <interceptor-ref name="servletConfig"/>
                <interceptor-ref name="i18n"/>
                <interceptor-ref name="chain"/>
                <interceptor-ref name="modelDriven"/>
                <interceptor-ref name="fileUpload"/>
                <interceptor-ref name="staticParams"/>
                <interceptor-ref name="conversionError"/>
                <interceptor-ref name="params"/>
                <interceptor-ref name="authz"/>
                <interceptor-ref name="user"/>
                <interceptor-ref name="prepare"/>
                <interceptor-ref name="validation"/>
                <interceptor-ref name="workflow"/>
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref name="defaultStack"/>

        <global-results>
            <result name="technicalError" type="chain">errorAction</result>
            <result name="sessionInvalidError" type="tiles">sessionInvalid</result>
            <result name="blank" type="tiles">blank</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="technicalError"/>
            <exception-mapping exception="com.omb.service.exception.UserSessionInvalidException" result="sessionInvalidError"/>

        </global-exception-mappings>

log4j.xml:

<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out"/>
    <layout class="org.apache.PatternLayout">
        <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
    </layout>
</appender>
<appender name="file" class="org.apache.log4j.FileAppender">
    <param name="File" value="${application.log4j.fileName}"/>
    <param name="Append" value="true"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{DATE}|%-5p|%37c|%m%n"/>
    </layout>
</appender>
<appender name="dailyfile" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="File" value="${application.log4j.fileName}"/>
    <param name="Append" value="true"/>
    <param name="DatePattern" value="'.'yyyy-MM-dd"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{DATE}|%-5p|%37c|%m%n"/>
    </layout>
</appender>

<root>
    <priority value="${application.log4j.logger.root}"/>
    <appender-ref ref="dailyfile"/>
</root>
<logger name="com.opensymphony.xwork2">
    <level value="${application.log4j.logger.com.opensymphony.xwork2}"/>
</logger>
<logger name="org.apache.struts2">
    <level value="${application.log4j.logger.org.apache.struts2}"/>
</logger>
<logger name="org.hibernate">
    <level value="ERROR"/>
</logger>
<logger name="org.springframework">
    <level value="ERROR"/>
</logger>

<logger name="com.omb">
    <level value="${application.log4j.logger.com.accor.scoring}"/>
</logger>
<logger name="com.omb.mapping.or">
    <level value="${application.log4j.logger.com.accor.scoring.mapping.or}"/>
</logger>
<logger name="org.acegisecurity">
    <level value="${application.log4j.category.net.sf.acegisecurity}"/>
</logger>
<logger name="com.omb.i18n">
    <level value="${application.log4j.logger.com.accor.scoring.i18n}"/>
</logger>

动作类:

package com.omb.controller.gm;


public class MyAction extends ActionSupport {

    /**
     * 
     * @see com.opensymphony.xwork2.ActionSupport#execute()
     */
    public String execute() {
        Object ob = null;
        ob.toString();
        return SUCCESS;
    }

}

1 个答案:

答案 0 :(得分:0)

我通过实现拦截器找到了一种解决方法:

package com.omb.controller.interceptor;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;


public class CustomExceptionInterceptor extends AbstractInterceptor {

    Log logger = LogFactory.getLog(CustomExceptionInterceptor.class);

    /**
     * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
     */
    @Override
    public String intercept(ActionInvocation invocation) {

        try {
            return invocation.invoke();
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            return "error";
        }
    }
}

struts.xml中的拦截器定义:

<interceptors>
            <interceptor name="authz" class="com.omb.controller.security.AuthzInterceptor"/>

            <interceptor name="params-filter" class="com.opensymphony.xwork2.interceptor.ParameterFilterInterceptor"/>
            <interceptor name="user" class="com.omb.controller.interceptor.UserInterceptor"/>
            <interceptor name="customException" class="com.omb.controller.interceptor.CustomExceptionInterceptor"/>

            <interceptor-stack name="defaultStack">
                <interceptor-ref name="exception">
                    <param name="exception.logEnabled">true</param>
                    <param name="exception.logLevel">ERROR</param>
                </interceptor-ref>
                <interceptor-ref name="alias"/>
                <interceptor-ref name="servletConfig"/>
                <interceptor-ref name="i18n"/>
                <interceptor-ref name="chain"/>
                <interceptor-ref name="modelDriven"/>
                <interceptor-ref name="fileUpload"/>
                <interceptor-ref name="staticParams"/>
                <interceptor-ref name="conversionError"/>
                <interceptor-ref name="params"/>
                <interceptor-ref name="authz"/>
                <interceptor-ref name="user"/>
                <interceptor-ref name="prepare"/>
                <interceptor-ref name="validation"/>
                <interceptor-ref name="workflow"/>
                <interceptor-ref name="customException"/>
            </interceptor-stack>
        </interceptors>