0

I want to extend Log4j Logger for a special purpose. Here is the class that I have written:

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);
    }
}

And I am calling it as:

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

But I am getting exception:

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:]

1 Answer 1

1

I don't see why you are trying to extend the Logger, try extending an AppenderSkeleton instead, which really is what is doing all the job. And then add that Appender to your config

public class MyAppender extends AppenderSkeleton {
    ...
}

And then add in your config:

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

I have tried this myself and it works like a charm.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but there is many reason why I want to extend it. Among them one of is JBoss 7 is not good for log4j. But it is out of topic, I just need to extend it for my programming purpose. Is it not possible?
It's probably possible, but it's probably not that easy. You will probably need to override the LogManager.getLogger(name) method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.