Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Classloading issue with OSGi

I have use log4j-1.2.17 bundle from Maven repo. I try to do this code in the bundle (my bundle calls to the log4j-1.2.17 bundle)

    PropertyConfigurator.configure(props());
private static Properties props() {
    Properties props = new Properties();
    props.put("log4j.rootLogger", "INFO, R");
    props.put("log4j.appender.R",
            "org.apache.log4j.DailyRollingFileAppender");
    props.put("log4j.appender.R.File", "logs/IhtikaClient.log");
    props.put("log4j.appender.R.Append", "true");
    props.put("log4j.appender.R.Threshold", "INFO");
    props.put("log4j.appender.R.DatePattern", "'.'yyyy-MM-dd");
    props.put("log4j.appender.R.layout", "org.apache.log4j.PatternLayout");
    props.put("log4j.appender.R.layout.ConversionPattern",
            //"%d{HH:mm:ss,SSS} %c - %m%n");
            //"[%5p] %d{yyyy-MM-dd mm:ss} (%F:%M:%L)%n%m%n%n");
            "[%5p] %d{yyyy-MM-dd mm:ss} %c (%F:%M:%L)%n%m%n");
    return props;
}

But I have error

log4j:ERROR A "org.apache.log4j.DailyRollingFileAppender" object is not assignable to a "org.apache.log4j.Appender" variable. log4j:ERROR The class "org.apache.log4j.Appender" was loaded by log4j:ERROR [org.apache.felix.framework.BundleWiringImpl@9da1dd] whereas object of type log4j:ERROR "org.apache.log4j.DailyRollingFileAppender" was loaded by [sun.misc.Launcher$AppClassLoader@4b222f]. log4j:ERROR Could not instantiate appender named "R". log4j:ERROR A "org.apache.log4j.DailyRollingFileAppender" object is not assignable to a "org.apache.log4j.Appender" variable. log4j:ERROR The class "org.apache.log4j.Appender" was loaded by log4j:ERROR [org.apache.felix.framework.BundleWiringImpl@9da1dd] whereas object of type log4j:ERROR "org.apache.log4j.DailyRollingFileAppender" was loaded by [sun.misc.Launcher$AppClassLoader@4b222f]. log4j:ERROR Could not instantiate appender named "R".

Bundles has loaded in the sequence

.getBundleContext().installBundle("......../log4j-1.2.17.jar")
.getBundleContext().installBundle("......../I_MainForm-1.0-SNAPSHOT.jar")

How to fix this error? Please, sorry my English. Best regards, Arthur.

like image 200
Arthur Avatar asked Sep 23 '26 02:09

Arthur


2 Answers

Basically, don't "leak" your classes into the framework using org.osgi.framework.system.packages or org.osgi.framework.system.packages.extra, unless you really need in your code instead of fixing some other issues. Apparently, it is log4j issue.

you can fix it by setting system property:

-Dlog4j.ignoreTCL=true 
like image 191
Sola Yang Avatar answered Sep 28 '26 16:09

Sola Yang


As your Log4j class seems to have been loaded by a sun.misc classloader, I think something is supplying the log4j classes from outside OSGi. The fact that there are Log4J entries explaining why it can not load Log4J classes means that some version of Log4J is already there.

Are you using a customized version of Felix? If so can you find something like a configuration file with an org.osgi.framework.system.packages=... entry or a org.osgi.framework.system.packages.extra=... entry?

What happens if you simply remove the Log4j bundle? Can it still find Log4j classes then?

Regards, Frank

like image 39
Frank Lee Avatar answered Sep 28 '26 16:09

Frank Lee