2

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.

Arthur
  • 3,253
  • 7
  • 43
  • 75

4 Answers4

7

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 
Sola Yang
  • 975
  • 8
  • 8
4

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

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
Frank Lee
  • 2,728
  • 19
  • 30
  • Hello. Yes, it's my mistake - the Main application (Loader application) has log4j lib (not bundle). After I has remove this lib, execption not shown now. Sorry my English. Thanks. – Arthur Jul 30 '12 at 11:13
  • @user913766: Please accept this answer as correct using the tick on the left. – Björn Pollex Jul 30 '12 at 17:41
1

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

Christian Schneider
  • 19,420
  • 2
  • 39
  • 64
0

This worked for me:

mvn install -D log4j.ignoreTCL=true