Apart from somehow incorportating Log4j (->external link) via providing it at some general library place of the OSGi-Container, and likewise with the logging configuration, which is not very OSGi-stylish, you can also use clean bundles to use it. For a basic setup we need three bundles: a general host bundle containing the log4j framework classes, a bundle with the configuration, and optionally a bundle for custom appenders if you need one. We will be using Eclipse Mars as an IDE and Log4j 1.2.9, but in principle you can substitute everything with the newer Log4j 2.x versions.
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Log4j-1.2 Bundle-SymbolicName: log4j12 Bundle-Version: 1.2.9 Export-Package: org.apache.log4j;version="1.2.9" Bundle-ClassPath: ., lib/log4j-1.2.9.jar
lib/ log4j-1.2.9.jar META-INF/ MANIFEST.MF(use ANT, or Maven, or Groovy-Antrunner).
This bundle can be used via importing its only package org.apache.log4j from any other bundle. This is not enough though, we still need a configuration, see next section.
log4j.rootLogger=DEBUG, Appender1, Appender2 log4j.appender.Appender1=org.apache.log4j.ConsoleAppender log4j.appender.Appender2=org.apache.log4j.RollingFileAppender log4j.appender.Appender2.File=sample.log log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout log4j.appender.Appender1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout log4j.appender.Appender2.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Log4jConf Bundle-SymbolicName: log4j-conf Bundle-Version: 1.0.0 Fragment-Host: log4j12;bundle-version=1.2.9 Bundle-ClassPath: .The important part is the Fragment-Host statement. Only then Log4j will be able to see the configuration from inside the Log4j framework bundle!
log4j.properties META-INF/ MANIFEST.MF(use ANT, or Maven, or Groovy-Antrunner).
Unless you need a custom appender, you're done. Install the two bundles and any other bundle can import the Log4j functionalities in its own MANIFEST.MF via:
Import-Package: ..., org.apache.log4j;version="[1.2.9,1.2.9]", ...
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: TestAppender Bundle-SymbolicName: test-appender Bundle-Version: 1.0.0 Fragment-Host: log4j12;bundle-version=1.2.9 Bundle-ClassPath: .
[The classes...] META-INF/ MANIFEST.MF(use ANT, or Maven, or Groovy-Antrunner).
Install that bundle in the OSGi container, and the new appender can be used from inside the Log4j configuration file.