我想扩展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:]
答案 0 :(得分:1)
我不明白为什么你要扩展Logger,尝试扩展一个AppenderSkeleton,这正是做所有工作的。然后将Appender添加到您的配置
public class MyAppender extends AppenderSkeleton {
...
}
然后添加你的配置:
log4j.rootLogger=MyApp
log4j.appender.MyApp=com.mypackage.MyAppender
我自己尝试过,它就像一个魅力。