0

我想为特殊目的扩展 Log4j Logger。这是我写的课程:

package com.edfx.adb.common.logger;

import javax.faces.application.ProjectStage;
import javax.faces.context.FacesContext;

import org.apache.log4j.LogManager;

public final class Logger extends org.apache.log4j.Logger {

    protected Logger(String name) {
        super(name);
    }

    public static org.apache.log4j.Logger getLogger(String name) {
        return LogManager.getLogger(name);
    }

    @Override
    public void debug(Object message) {
        if (isDebugEnable()) {
            super.debug(message);
        }
    }

    @Override
    public void debug(Object message, Throwable throwable) {
        if (isDebugEnable()) {
            super.debug(message, throwable);
        }
    }

    private boolean isDebugEnable() {
        return FacesContext.getCurrentInstance().isProjectStage(ProjectStage.Development);
    }
}

我称它为:

protected final Logger log = (Logger) Logger.getLogger(getClass());

但我得到了例外:

Caused by: java.lang.ClassCastException: org.jboss.logmanager.log4j.BridgeLogger cannot be cast to com.edfx.adb.common.logger.Logger
        at com.edfx.adb.web.controller.BaseWebController.<init>(BaseWebController.java:18) [classes:]
        at com.edfx.adb.web.controller.AuthController.<init>(AuthController.java:27) [classes:]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [rt.jar:1.7.0_09]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) [rt.jar:1.7.0_09]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) [rt.jar:1.7.0_09]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:525) [rt.jar:1.7.0_09]
        at java.lang.Class.newInstance0(Class.java:372) [rt.jar:1.7.0_09]
        at java.lang.Class.newInstance(Class.java:325) [rt.jar:1.7.0_09]
        at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:188) [jsf-impl-2.1.7-jbossorg-2.jar:]
4

1 回答 1

1

我不明白您为什么要尝试扩展 Logger,而是尝试扩展 AppenderSkeleton,这确实是完成所有工作的原因。然后将该 Appender 添加到您的配置中

public class MyAppender extends AppenderSkeleton {
    ...
}

然后添加您的配置:

log4j.rootLogger=MyApp
log4j.appender.MyApp=com.mypackage.MyAppender

我自己试过这个,它就像一个魅力。

于 2012-12-09T17:53:56.830 回答