Log4j2无法通过附加程序记录Faces异常

时间:2018-10-01 09:55:11

标签: log4j2 java-ee-7 myfaces

我有一个具有不同应用程序的tomcat服务器,每个服务器都有它自己的log4j2附加程序,该附加程序将日志记录到自己的文件中,每个java类都正确记录了它的异常,对于JSF异常(例如未关闭的标签,错误的类/方法/属性)名称,我做了一个defaultExceptionhandler来记录FacesExceptions,但是由于某种原因我找不到它根本不记录日志。

这是环境的规格 Ubuntu 18.04和16.04(以防万一) 雄猫8.0.47 甲骨文jdk 1.8.0_181 myfaces 2.3.1 log4j2 2.11.0

所以事情是,即使在调试handle()的同时通过保持标记打开等强制异常的情况下,它也确实达到了logger.error()调用上的断点,但只是没有追加到文件中

这是该项目的faces-config.xml中处理程序的声明(到目前为止,我已经保留了所有导航用例和其他垃圾,所有这些都可以使用,如果需要,我将对其进行编辑)

    <faces-config version="2.0"
                      xmlns="http://java.sun.com/xml/ns/javaee" 
                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">

        <factory>
            <exception-handler-factory>
                gescoweb.tools.DefaultExceptionHandlerFactory
            </exception-handler-factory>
        </factory>       

    </faces-config>

这是DefaultExceptionHandler.java

package gescoweb.tools;

import java.util.Iterator;
import javax.faces.FacesException;
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerWrapper;
import javax.faces.context.FacesContext;
import javax.faces.event.ExceptionQueuedEvent;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

/**
 *
 */
public class DefaultExceptionHandler extends ExceptionHandlerWrapper {

    private ExceptionHandler wrapped;

    public static final Logger LOGGER = LogManager.getLogger("gescoweb");

    public DefaultExceptionHandler(ExceptionHandler wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public ExceptionHandler getWrapped() {
        return this.wrapped;
    }

    /** Recibe la lista de excepciones y las procesa según su tipo.
     * @throws FacesException 
     */
    @Override
    public void handle() throws FacesException {
        for (Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {
            Throwable t = i.next().getContext().getException();
            if (t != null) {
                LOGGER.error("Error inesperado.", new Exception(t));
            } else {
                LOGGER.error("", new Exception(t));
            }
        }
        getWrapped().handle();
    }

    /**
     * Tratamiento especial de otras excepciones.
     * @param facesContext
     * @param t
     * @return 
     */
    protected String handleUnexpected(FacesContext facesContext, final Throwable t) {
        if (t instanceof IllegalStateException) {
            return "key.exception.IllegalStateException";
        } else {
            super.handle();
            return "";
        }
    }
}

这是处理程序工厂

package gescoweb.tools;

import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerFactory;

/**
 *
 */
public class DefaultExceptionHandlerFactory extends ExceptionHandlerFactory {

    private ExceptionHandlerFactory parent;

    public DefaultExceptionHandlerFactory(ExceptionHandlerFactory parent) {
        this.parent = parent;
    }

    /**
     * Crea los handlers modificados.
     *
     * @return
     */
    @Override
    public ExceptionHandler getExceptionHandler() {
        ExceptionHandler eh = parent.getExceptionHandler();
        eh = new DefaultExceptionHandler(eh);
        return eh;

    }
}   

最后这是log4j2.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Properties>
        <Property name="log-path">${sys:catalina.base}/logs</Property>
        <Property name="layout"> [%-5level] %d{yyyy-MM-dd HH:mm:ss} %c{1} - %msg%ex%n </Property>
    </Properties>

    <Appenders>
        <!-- console appender -->
        <Console name="console-log" target="SYSTEM_OUT">
            <PatternLayout pattern="${layout}"/>
        </Console>

        <!-- logger gescoweb -->
        <RollingFile name="gescoweb-log" fileName="${log-path}/gescoweb.log"
                     filePattern="${log-path}/gescoweb/$${date:yyyy-MM}/gescoweb-%d{dd}.log.gz">
            <!-- log pattern -->
            <PatternLayout pattern="${layout}"/>
            <!-- set file size policy -->
            <CronTriggeringPolicy schedule="0 0 12 */7 * ?"/>
        </RollingFile>

        <!-- logger catalina -->
        <RollingFile name="catalina-log"
                     fileName="${log-path}/catalina.out"
                     filePattern="${log-path}/catalina/$${date:yyyy-MM}/catalina-%d{dd}.log.gz">
            <PatternLayout pattern="${layout}"/>
            <CronTriggeringPolicy schedule="0 0 12 */7 * ?"/>
        </RollingFile>

        <!-- logger localhost -->
        <RollingFile name="localhost-log"
                     fileName="${log-path}/localhost.log"
                     filePattern="${log-path}/localhost/$${date:yyyy-MM}/localhost-%d{dd}.log.gz">
            <PatternLayout pattern="${layout}"/>
            <CronTriggeringPolicy schedule="0 0 12 */7 * ?"/>
        </RollingFile>

        <!-- logger manager -->
        <RollingFile name="manager-log"
                     fileName="${log-path}/manager.log"
                     filePattern="${log-path}/manager/$${date:yyyy-MM}/manager-%d{dd}.log.gz">
            <PatternLayout pattern="${layout}"/>
            <CronTriggeringPolicy schedule="0 0 12 */7 * ?"/>
        </RollingFile>

        <!-- logger host-manager -->
        <RollingFile name="host-manager-log"
                     fileName="${log-path}/host-manager.log"
                     filePattern="${log-path}/host-manager/$${date:yyyy-MM}/host-manager-%d{dd}.log.gz">
            <PatternLayout pattern="${layout}"/>
            <CronTriggeringPolicy schedule="0 0 12 */7 * ?"/>
        </RollingFile>
    </Appenders>

    <Loggers>

        <Logger name="gescoweb" level="debug">
            <AppenderRef ref="gescoweb-log"/>
        </Logger>
        <Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost]"
                level="info"
                additivity="false">
            <AppenderRef ref="localhost-log"/>
        </Logger>
        <Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]"
                level="info"
                additivity="false">
            <AppenderRef ref="manager-log"/>
        </Logger>
        <Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]"
                level="info"
                additivity="false">
            <AppenderRef ref="host-manager-log"/>
        </Logger>
        <Root level="info">
            <AppenderRef ref="console-log"/>
            <AppenderRef ref="catalina-log"/>
        </Root>
    </Loggers>
</Configuration>

1 个答案:

答案 0 :(得分:0)

没关系,我在DefaultExceptionHandler中导入了错误的log4j库,将导入更改为

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

现在一切正常...