9

After many tests i concluded that the Log4j is not working under OSGI.

I do not use Log4j directly, but i need third party plugins to log with it.

I made a plain JAVA project and everything works great, but under PLUGIN development nothing works.

I have log4j jar in my Classpath and even tried a Spring repository OSGI compliant Log4j and included it under Dependencies. Nothing works.

I've tried this some approach with no success: http://swik.net/Eclipse/Planet+Eclipse/Raja+Kannappan:+Eclipse+RCP+-+Converting+Dependencies+to+OSGi+Bundles/drqpf

and could not fully understand this one: http://www.eclipsezone.com/eclipse/forums/t99588.html

Can anyone point me on the right direction?

Thanks.

marcolopes
  • 9,232
  • 14
  • 54
  • 65
  • UPDATE! I've found an alternative: EDIT an OSGI compliant version of log4j (get it from SPRING repository) with winrar, PASTE the log4j properties files to the ROOT of the JAR. Import the package in your plugin DEPENDENCIES, et voilá! Each approach has it's advantages. (Don't forget Eugener's IMPORTANT TIP, that makes it all work: All the bundles which use log4j should have org.apache.log4j in their Import-Package attribute) – marcolopes Mar 03 '11 at 11:10

2 Answers2

14

You simply have to create log4j fragment bundle with the log4j.properties file inside.

UPDATE: Some things to look for:

  • Header name should be: Fragment-Host: log4j.
  • log4j.properties should be in the src folder.
  • All the bundles which use log4j should have org.apache.log4j in their Import-Package attribute
Eugene Ryzhikov
  • 17,131
  • 3
  • 38
  • 60
  • Did that. Fragment with log4j jar in the classpath and log4j properties on the fragment root. Imported fragment to main APP product. No success... – marcolopes Mar 02 '11 at 14:50
  • 1
    Datanucleus Plugin does not have org.apache.log4j in the "Import-Package"... also, i cannot set the "Fragment-Host" to "log4j" because i need to specify the "HOST-Plugin" (and i cannot change the name of my main project!) – marcolopes Mar 02 '11 at 20:26
  • YES, YES, YES! One year looking after this solution! Thanks a lot! Your tips were just right. I had to edit all the MANIFEST plugins and add "org.apache.log4j" to the Import-Package Section (winrar did a nice job saving the manifest into the JAR). The fragment header is related to the Host Plugin, and it works nicely! Thanks again! – marcolopes Mar 02 '11 at 21:42
  • hard to say... possibly duplicate configuration, but not sure – Eugene Ryzhikov Mar 03 '11 at 01:50
  • Found the problem: "additivity" property must be set (to false) or logs will be "added" to "parent" category. – marcolopes Mar 09 '11 at 00:29
1

For logging in OSGi, you should take a look at OSGi Log Service. Here is article that explains quite well a good approach: http://blog.kornr.net/index.php/2008/12/18/osgi-logging-putting-it-all-together

You can also take a look at Pax-Logging: http://wiki.ops4j.org/display/paxlogging/Pax+Logging

RaduK
  • 1,463
  • 10
  • 16
  • The purpose is not to create a log service, but for a third-party plugin to log... – marcolopes Mar 02 '11 at 14:51
  • Make sure you have the bundle log4j ACTIVE in your OSGi platform. (having it in the classpath is not enough) Then you can use the fragment approach for properties file if you want. – RaduK Mar 02 '11 at 16:04