NOTE: This article is a replica of the work by Pierre Parrend and published on his website. This replica is made by his expressed consent and with the purpose of a backup in case the original is ever decommissioned.
The previous technical note on java loggingpresents the principles of code logging, as well as some of the most widespread logging libraries for java: Sun J2SE Logging API, Apache Log4J framework, Simple Log library, Jakarta Common Logging API, Simple Logging Facade for Java, X4juli. The configuration of the logging is done through a unique config file for instance log4j/log4j.properties.
In the context of an OSGi platform, each bundle (i.e. OSGi archives) have its own view on the file system, so using a centralized config file can not conduct to anything but dirty hacks. The solution is provided by the Pax logging facility, which bundelizes both the library code and the configuration file.
This technical note presents a Quick Start view on using the Pax logging library: what steps are required to let a proper logging be available on your OSGi platform ?
Following aspects will be shown:
This Quick Start assumes that you are ready to install suitable code for installing OSGi and the Pax library.
We use the Felix OSGi implementation.
The Maven development environment is used for compiling both Felix and Pax.
All necessary logging libraries are provided in Pax. We provide some convenient extension.
Restriction: no other logging API should be available in the platform.
This section introduces how to install, configure and launch a OSGi platform with the Pax logging facility.
Of coure, you can just skip the items that are already available on your system.
svn co https://svn.apache.org/repos/asf/incubator/felix/trunk/tools/maven2 |
svn co http://svn.apache.org/repos/asf/incubator/felix felix |
mkdir ops4j |
cd ./base |
cd ./logging/ mvn clean install |
|
You have just installed maven, felix, pax, and started the pax bundles that are required for performing logging with log4j in a OSGi platform.
This section introduces how to configure, install and test the log4j facility. An example highlight the behavior of log4j with Pax.
start file:///XX/logging/log4jclient/target/log4jclient-0.1.jar |
2 loggers (demoLogger, and develLogger) are created, which each issue an error and a warning,
the configuration is set so that the demoLogger print logs that are at least as serious logging/log4jclient/target/log4jclient-0.1.jaras ERROR, and the develLogger prints logs that are at least as serious as DEBUG.
This section introduces how to launch the standard example provided with Pax, that uses simultaneously Jcl, Avalon, Sl4j and Jdk logger with a log4j backup.
OSGi Service, OSGi Util, Servlet
from OBR,
or manually from following URL: http://oscar-osgi.sourceforge.net/
or in the log4jlogging.doc/repository/ directory.
start file:///XX/logging/avalon/target/avalon-0.9.5-SNAPSHOT.jar |
cd logging/example |
<osgi-BundleActivator>org.ops4j.pax.logging.example.Activator</osgi-BundleActivator> |
mvn clean install |
start file://XX/logging/example/target/example-0.9.5-SNAPSHOT.jar |
This section introduces how to use the Pax facility in an existing Maven project.
<dependency> |
Logger.getLogger("name"); |
Related tutorials
External resources