Classloading issue with OSGi

旧时模样 提交于 2020-01-02 15:13:40

问题


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.


回答1:


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




回答2:


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 



回答3:


Setting up log4j in your own code is not a good idea in OSGi. You should take a look at ops4j pax logging. It takes care of the log framework setup and you can simply use the log4j api in your bundle. See: http://team.ops4j.org/wiki/display/paxlogging/Pax+Logging



来源:https://stackoverflow.com/questions/11719218/classloading-issue-with-osgi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!