ops4j / org.ops4j.pax.logging

The OSGi Logging framework implementation. Supports SLF4J,LOG4J,JCL etc.
https://ops4j1.jira.com/wiki/spaces/paxlogging/overview
Apache License 2.0
46 stars 79 forks source link

= PAX-LOGGING

The goal of this project is to allow users and bundles to use well known Logging APIs like SLF4J or Commons-Logging.

== Versions

|=== | |1.9.x |1.10.x |1.11.x |1.12.x |2.0.x |2.1.x |2.2.x

|OSGi Log version |R6 (1.3) |R6 (1.3) |R6 (1.3) |R6 (1.3) |R7 (1.4) |R7 (1.4) |R8 (1.5)

|Log4j1 backend |yes |yes |yes |no |yes |no |no

|Log4j2 version |2.12.4 |2.17.1 |2.17.1 |2.22.1 |2.18.0 |2.18.0 |2.23.1

|Logback version |no |1.2.9 |1.2.11 |1.3.14 |1.2.11 |1.2.11 |1.3.14

|Maintained |no |no |no, use 1.12.x instead |yes |no, use 2.2.x instead |no, use 2.2.x instead |yes |===

NOTE: All versions support Log4j1 API, while only selected ones support Log4j1 backend.

.History of the versions

At some point, https://reload4j.qos.ch/[reload4j] fork of https://logging.apache.org/log4j/1.2/index.html[Apache log4j] was used.

In 2023 I decided to stop maintaining these versions:

1.12.x should be used for older OSGi runtimes, based for example on Karaf 4.2 (and Felix 5.6.12).

Please don't worry about deprecating 2.0.x/2.1.x. Pax Logging 2.2.x is the same as 2.1.x, but with two exports:

... org.osgi.service.log; version=1.4, org.osgi.service.log; version=1.5, ...

== Basics

There should be a distinction between Logging API which, by itself, doesn't do any logging, but instead requires specific logging implementation (and related configuration) and the Logging implementation (referred to as a backend) itself.

Logging APIs (or Facades) matching the above distinction include:

Logging implementations always provide their own APIs and can be used without any of the above facades. These include:

However usage of Logback without SLF4J is very rare and won't be implemented in pax-logging.

And the above two (with their specific configuration file syntax) are supported by (respectively):

pax-logging-log4j1 was named pax-logging-service before version 2.0.0 and was removed completely in 2.1.0. However Log4j1 API was not removed.

== Different Logging APIs

The fundamental interface is org.osgi.service.log.LogService from Chapter 101 of OSGi Compendium specification. The history of org.osgi.service.log package versions is:

pax-logging-api 1.x uses version 1.3 from OSGi Compendium R6.

pax-logging-api 2.0.x/2.1.x uses version 1.4 from OSGi Compendium R7.

pax-logging-api 2.2.x uses version 1.5 from OSGi Compendium R8.

Each class that's an entry point to specific Logging framework (like org.slf4j.LoggerFactory) interacts with pax-logging using org.ops4j.pax.logging.PaxLoggingManager. This manager tracks instances of framework-specific org.ops4j.pax.logging.PaxLoggingService services that eventually delegate to particular framework (like Log4J2).

This indirection allows to plug-in OSGi specific mechanism (like replacing org.ops4j.pax.logging.PaxLoggingService implementation at runtime) without refreshing (in OSGi terms) pax-logging-api bundle.

There's one important thing. Logging facades and frameworks share familiar concepts of category (or a logger name). This concept was introduced (afair) in Log4J1 with tree-like hierarchy of loggers where more general loggers could provide shared configuration (like appenders) for more particular loggers.

OSGi Compendium org.osgi.service.log.LogService doesn't have this notion of category, but (without a need to provide more technical details) pax-logging implements calls to org.osgi.service.log.LogService.log() method in such way that category is (if possible) bundle's symbolic name. This category is then used to get a logger from underlying logging backend.

In other words each org.osgi.service.log.LogService.log() call may be represended by this pseudo code:

[listing,options=nowrap]

// calculate category category = try to take category from bundle's symbolic name

// obtain logger from underlying backend, like Log4J2 logger = LoggerFactory.getLogger(bundle, category)

// log a message according to specified level logger.info(message, throwable) // or warn() or debug(), ...

Being aware of this, it's actually better to not use org.osgi.service.log.LogService.log() in your code directly and just use given logging facade (like SLF4J or Commons Logging) - this removes a need to obtain a logger (which is cached anyway) on each log call.

=== OSGi R7 Log Service

With OSGi R7, standard Log Service gets a concept of logger. This makes the service easier to use - almost like de facto standard logging facades, where some factory is used to obtain loggers that are used to invoke logging methods.

OSGi R7 adds new interface org.osgi.service.log.LoggerFactory, which is actually a new super interface of old org.osgi.service.log.LogService service.

Factory methods that return instances of org.osgi.service.log.Logger by name or class are intuitive. There are however factory methods that take new argument: Class<L> loggerType where <L extends org.osgi.service.log.Logger>. The only known/acceptable classes that can be passed to such factory methods are:

By passing org.osgi.service.log.FormatterLogger.class as an argument, user of factory method indicates that he/she wants to use printf-like formatting, for example:

[listing,options=nowrap]

ServiceReference sr = context.getServiceReference(org.osgi.service.log.LoggerFactory.class); LoggerFactory loggerFactory = context.getService(sr); org.osgi.service.log.Logger log = loggerFactory.getLogger("com.example.service", FormatterLogger.class); log.info("Hello %s", "world");

When passing org.osgi.service.log.Logger.class or when not passing anything, we assume Slf4J-like formatting:

[listing,options=nowrap]

ServiceReference sr = context.getServiceReference(org.osgi.service.log.LoggerFactory.class); LoggerFactory loggerFactory = context.getService(sr); org.osgi.service.log.Logger log = loggerFactory.getLogger("com.example.service"); log.info("Hello {}", "world");

==== Fully Qualified Class Name

FQCN concept is very important. It's used to mark a place inside stack trace where code that invokes logging operation transitions into the logging mechanism itself. Last stack frame before logging mechanism marks a location that can be used to obtain class name, method name, file name and line number (so called location info). Before OSGi R7, it was quite easy to implement correctly, because all the logging methods where managed by Pax Logging itself and proper FQCN could be set. With OSGi R7 logging service, logger is also a standard interface and logging methods may be called without any facade (like Slf4J).

Pax Logging had to carefuly handle all the entry points into logging mechanism, to correctly determine FQCN.

== Concepts

pax-logging is not trivial bridge to different (2 actually) logging frameworks (Log4J2, Logback), it also provides SPI layer that allows users to extend any logging framework. The most clear usage is when user wants to provide custom appender that processes logging events.

=== Logging events

Each time a method like log.info() is called in any code fragment, new logging event is created. This event is a representation of a fact that user wants to log some message/information. Each logging event has some information associated like timestamp, code location (if available), message, exception (if needed) and severity (importance) of the event.

=== Appenders

Appender is kind of processor that does something with the logging event - usually appending new information (typically a line) to a file.

=== Level and threshold

This is a bit confusing part. At first glance it looks trivial - when user wants to invoke logging method, he/she may use one of typical methods like info(), warn(), trace() or other.

Simply from this set of alternatives we can derive the concept of logging event level (or severity).

Let's start with something we can treat as canonical - https://en.wikipedia.org/wiki/Syslog#Severity_level[Syslog]. Here are the level names and their numerical equivalents:

.Syslog levels |=== |Numerical value |Severity/level name

|0 |Emergency

|1 |Alert

|2 |Critical

|3 |Error

|4 |Warning

|5 |Notice

|6 |Informational

|7 |Debug |===

From the above, we can try to summarize:

==== Numerically higher level is less important.

A concept related to level is threshold. Without giving precise constraints, threshold is a limit for logging events. For the above, Syslog example, setting a threshold value to Warning means that we're interested in events with Warning or (numerically) lower events.

Thus:

==== The higher (numerically) the threshold, the more logging events are processed. Less important events are processed.

.Adding more confusion

Logging frameworks (and APIs) used in pax-logging treat the level concept differently... Log4J1 has direct relation to Syslog levels, but it's not a case with Log4J2 and java.util.logging. Here's a table where Syslog and Log4J1 can be directly related. Placement of levels from other libraries is a bit arbitrary and related to logging level name equivalents.

[options=nowrap] |=== |Syslog |Log4J1 |Log4J2 |Logback|java.util.logging |SLF4J |OSGi R6|OSGi R7

0 - Emergency Integer.MAX_VALUE - OFF 0 - OFF Integer.MAX_VALUE - OFF Integer.MAX_VALUE - OFF
0 - AUDIT
0 - Emergency 50000 - FATAL 100 - FATAL
1000 - SEVERE
1 - Alert
2 - Critical

|3 - Error |40000 - ERROR |200 - ERROR |40000 - ERROR |1000 - SEVERE |40 - ERROR |1 - ERROR |1 - ERROR

|4 - Warning |30000 - WARN |300 - WARN |30000 - WARN |900 - WARNING |30 - WARN |2 - WARNING |2 - WARN

5 - Notice

|6 - Informational |20000 - INFO |400 - INFO |20000 - INFO |800 - INFO, 700 - CONFIG |20 - INFO |3 - INFO |3 - INFO

|7 - Debug |10000 - DEBUG |500 - DEBUG |10000 - DEBUG |500 - FINE |10 - DEBUG |4 - DEBUG |4 - DEBUG

7 - Debug 5000 - TRACE 600 - TRACE 5000 - TRACE 400 - FINER 0 - TRACE
5 - TRACE
300 - FINEST
7 - Debug Integer.MIN_VALUE - ALL Integer.MAX_VALUE - ALL Integer.MIN_VALUE - ALL Integer.MIN_VALUE - ALL
===

Notes and confusing parts:

=== Markers

Markers allow to pass/associate additional, dynamic information with logging operation itself. Just as logger name (category) and level are static aspects of the logger itself, marker is associated with single logging invocation (thus effectively with logging event). Single logger may be used to log message with or without marker and it's up to specific implementation (Logback, Log4J2) to handle the marker accordingly.

For example, Log4J1 doesn't support markers, so slf4j-log4j12 bridges to Log4J1 using org.slf4j.helpers.MarkerIgnoringBase abstract base class which simply ignores markers. Logback and Log4J2 implement full org.slf4j.spi.LocationAwareLogger with marker support.

Markers are used usually by implementation-specific filters and appenders:

Finally, a marker may have parent (or child) marker(s) associated - making them something slightly more complex than single name.

In Pax Logging, org.ops4j.pax.logging.PaxLogger interface didn't contain methods accepting markers. https://ops4j1.jira.com/browse/PAXLOGGING-160[PAXLOGGING-160] passed marker as String attribute through thread-bound org.ops4j.pax.logging.PaxContext. https://ops4j1.jira.com/browse/PAXLOGGING-259[PAXLOGGING-259] adds such methods to this interface.

Remember - in Pax Logging, it's possible to use for example Log4J2 API to log information that's effectively handled by Logback, so despite the API being aware of markers, they may not be used correctly by actual logging implementation. As consequence, isXXXEnabled(..., marker, ...) methods may not be handled early in the process of logging.

== SLF4J

slf4j-api-1.7.33-sources.jar contains more sources than slf4j-api-1.7.33.jar has classes - in particular, org.slf4j.impl package is removed from the jar and the responsibility to provide:

classes lies on the side of binding library for SLF4J API. Such classes are provided by (among others):

pax-logging-api provides own implementation of these three classes. All other classes are directly repackaged (using bndlib) from slf4j-api-1.7.33.jar - classes that don't have to be changed are no longer shipped in pax-logging-api source directory.

== Commons Logging

While SLF4J takes simple and elegant approach for finding the actual implementation (StaticLoggerBinder), Commons Logging uses old school discovery through various ClassLoader and ServiceLoader tricks.

In pax-logging, all this discovery is not needed, so the only reimplemented class is org.apache.commons.logging.LogFactory with all the discovery code removed.

== Apache JULI

Apache JULI is specialized (and repackaged) version of Commons Logging with original discovery mechanism already removed for Tomcat's internal logging mechanism purposes.

In pax-logging, there was less work to do - discovery mechanism was already removed, only org.apache.juli.logging.LogFactory.getInstance(java.lang.String) method was changed to delegate to PaxLoggingManager.

== Avalon Logging

Ancient Avalon framework predates most (if not all) Java server frameworks aiming to provide code and component organization patterns and programming model. Without dealing much with archeology, pax-logging-api provides support for org.apache.avalon.framework.logger package where the ultimate source of truth is https://svn.apache.org/repos/asf/excalibur/tags/avalon-framework-api-4.3-Release/framework/api/src/java/org/apache/avalon/framework/logger/[this SVN tag and directory].

There are no factory methods to access Avalong loggers as we know from SLF4J or even from Commons Logging. There's simply new instance creation, where the reference may be assigned to org.apache.avalon.framework.logger.Logger interface. Thus pax-logging-api doesn't include any source from Avalon Framework. Simply implementation of org.apache.avalon.framework.logger.Logger is provided.

Excalibur (actual library/framework using Avalon) simply provides concrete implementations of org.apache.avalon.framework.logger.Logger, like:

To achieve factory method approach, pax-logging-api exports org.ops4j.pax.logging.avalon package with special (not implied from Avalon Framework design) factory class for Avalon loggers. For other facades, package with factory classes is not org.ops4j.pax.logging.*.

== JBoss Logging

JBoss started to use dedicated logging bridge (facade) with http://docs.jboss.org/hibernate/orm/4.3/topical/html/logging/Logging.html[Hibernate 4.0]. Similarly to e.g., Commons Logging, actual logging framework is discovered at runtime.

JBoss Logging can delegate to either concrete logging implementation (like Log4J2) or another logging facade (like SLF4J or Commons Logging). It uses discovery (ClassLoader + ServiceLoader) mechanism to find the framework to delegate to.

Originally, org.jboss.logging.provider property may be set to one of these values:

Then discovery checks ServiceLoader for org.jboss.logging.Provider provider (/META-INF/services/org.jboss.logging.Provider).

pax-logging API doesn't yet delegate JBoss Logging API to pax-logging OSGi manager. https://ops4j1.jira.com/browse/PAXLOGGING-251[PAXLOGGING-251] tracks this issue.

== Log4j

Ah, the grandfather of all configurable Logging frameworks. Created when there was no logging bridges/facades around. Actually first facade (Commons Logging) was created to bridge common logging API to one of different logging frameworks (back then, it was only Log4J1 and Java Util Logging (JUL) from JDK1.4).

Because its origins are in pre-logging bridge times, Log4J1's API was used directly by very large amount of code. That's why pax-logging fully supports its native API. However in Pax Logging 1.12.x and 2.1.x I've removed the implementation (in particular the appenders) based on Log4J1.

Also, this was the first logging framework embraced by pax-logging project itself.

Here, the problem is with splitting original log4j:log4j JAR into API (for pax-logging-api) and implementation (for pax-logging-log4j1).

The original Export-Package header of log4j:log4j (yes - it is correct OSGi bundle) is (after formatting):

[listing,options=nowrap]

org.apache.log4j; version="1.2.17"; uses:="org.apache.log4j.spi,org.apache.log4j.helpers,org.apache.log4j.pattern,org.apache.log4j.or,org.apache.log4j.config", org.apache.log4j.config; version="1.2.17"; uses:="org.apache.log4j.helpers,org.apache.log4j,org.apache.log4j.spi", org.apache.log4j.helpers; version="1.2.17"; uses:="org.apache.log4j,org.apache.log4j.spi,org.apache.log4j.pattern", org.apache.log4j.jdbc; version="1.2.17"; uses:="org.apache.log4j,org.apache.log4j.spi", org.apache.log4j.jmx; version="1.2.17"; uses:="org.apache.log4j,javax.management,org.apache.log4j.helpers,org.apache.log4j.spi", org.apache.log4j.net; version="1.2.17"; uses:="org.apache.log4j,org.apache.log4j.spi,javax.naming,org.apache.log4j.helpers,javax.jms,org.apache.log4j.xml,javax.mail,javax.mail.internet,org.w3c.dom,javax.jmdns", org.apache.log4j.nt; version="1.2.17"; uses:="org.apache.log4j.helpers,org.apache.log4j,org.apache.log4j.spi", org.apache.log4j.or; version="1.2.17"; uses:="org.apache.log4j.helpers,org.apache.log4j.spi,org.apache.log4j", org.apache.log4j.or.jms; version="1.2.17"; uses:="org.apache.log4j.helpers,javax.jms,org.apache.log4j.or", org.apache.log4j.or.sax; version="1.2.17"; uses:="org.apache.log4j.or,org.xml.sax", org.apache.log4j.pattern; version="1.2.17"; uses:="org.apache.log4j.helpers,org.apache.log4j.spi,org.apache.log4j,org.apache.log4j.or", org.apache.log4j.rewrite; version="1.2.17"; uses:="org.apache.log4j,org.apache.log4j.spi,org.apache.log4j.helpers,org.apache.log4j.xml,org.w3c.dom", org.apache.log4j.spi; version="1.2.17"; uses:="org.apache.log4j,org.apache.log4j.helpers,org.apache.log4j.or", org.apache.log4j.varia; version="1.2.17"; uses:="org.apache.log4j.spi,org.apache.log4j,org.apache.log4j.helpers" org.apache.log4j.xml; version="1.2.17"; uses:="javax.xml.parsers,org.w3c.dom,org.xml.sax,org.apache.log4j.config,org.apache.log4j.helpers,org.apache.log4j,org.apache.log4j.spi,org.apache.log4j.or",

Additionally, the jar contains:

pax-logging-api exports these (from log4j1):

[listing,options=nowrap]

org.apache.log4j; version=1.2.15; uses:="org.apache.log4j.spi org.ops4j.pax.logging org.osgi.framework" org.apache.log4j.spi; version=1.2.15; uses:="org.apache.log4j" org.apache.log4j.xml; version=1.2.15; uses:="javax.xml.parsers org.w3c.dom"

I checked original log4j:log4j and started with single reexport of org.apache.log4j package. The closure of exports turned out to be: [listing,options=nowrap]

Export-Package: org.apache.log4j; version="1.2.17"; uses:="org.apache.log4j.helpers,org.apache.log4j.or,org.apache.log4j.spi", org.apache.log4j.config; version="1.2.17"; uses:="org.apache.log4j", org.apache.log4j.helpers; version="1.2.17"; uses:="org.apache.log4j,org.apache.log4j.spi", org.apache.log4j.or; version="1.2.17"; uses:="org.apache.log4j.spi", org.apache.log4j.pattern; version="1.2.17"; uses:="org.apache.log4j,org.apache.log4j.helpers,org.apache.log4j.spi", org.apache.log4j.spi; version="1.2.17"; uses:="org.apache.log4j,org.apache.log4j.or", org.apache.log4j.xml; version="1.2.17"; uses:="org.apache.log4j,org.apache.log4j.config,org.apache.log4j.spi" Import-Package: com.ibm.uvm.tools;resolution:=optional

com.ibm.uvm.tools was additional import generated by analyzing (bndlib) org.apache.log4j.spi.LocationInfo class.

So the remaining exports from original log4j:log4j that are not part of the above closure are: [listing,options=nowrap]

org.apache.log4j.jdbc; version="1.2.17"; uses:="org.apache.log4j,org.apache.log4j.spi", org.apache.log4j.jmx; version="1.2.17"; uses:="org.apache.log4j,javax.management,org.apache.log4j.helpers,org.apache.log4j.spi", org.apache.log4j.net; version="1.2.17"; uses:="org.apache.log4j,org.apache.log4j.spi,javax.naming,org.apache.log4j.helpers,javax.jms,org.apache.log4j.xml,javax.mail,javax.mail.internet,org.w3c.dom,javax.jmdns", org.apache.log4j.nt; version="1.2.17"; uses:="org.apache.log4j.helpers,org.apache.log4j,org.apache.log4j.spi", org.apache.log4j.or.jms; version="1.2.17"; uses:="org.apache.log4j.helpers,javax.jms,org.apache.log4j.or", org.apache.log4j.or.sax; version="1.2.17"; uses:="org.apache.log4j.or,org.xml.sax", org.apache.log4j.rewrite; version="1.2.17"; uses:="org.apache.log4j,org.apache.log4j.spi,org.apache.log4j.helpers,org.apache.log4j.xml,org.w3c.dom", org.apache.log4j.varia; version="1.2.17"; uses:="org.apache.log4j.spi,org.apache.log4j,org.apache.log4j.helpers"

Not exported packages:

pax-logging-log4j1 (before it was removed) did not export anything.

Additionally, apache-log4j-extras-1.2.17.jar has some new packages:

OSGi Exported:

Not OSGi exported:

apache-log4j-extras-1.2.17.jar duplicates some packages from log4j-1.2.17.jar, but with additional classes (most of the classes are the same):

With PAXLOGGING-252, I'd like to make it easier to maintain pax-logging itself. The goals (and kind of work log) are:

.Update

My plan was to export the above set of packages from pax-logging-api and import them in pax-logging-log4j1 with few exceptions. Mainly, org.apache.log4j.Logger class has to be exported by pax-logging-api (with changes related to delegation to pax-logging services), but it also has to be private packaged in pax-logging-log4j1, because it actually has to call log4j:log4j functionality (like keeping hierarchy of loggers).

OSGi R6 Core specification says:

==== 3.9.4 Overall Search Order

Frameworks must adhere to the following rules for class or resource loading. When a bundle's class loader is requested to load a class or find a resource, the search must be performed in the following order:

3. If the class or resource is in a package that is imported using Import-Package or was imported dynamically in a previous load, then the request is delegated to the exporting bundle's class loader [...]

...

5. Search the bundle's embedded classpath.

So it was not possible:

The only solution is to not import org.apache.log4j package from pax-logging-api to pax-logging-log4j1 bundle. Some Maven tricks (maven-dependency-plugin:unpack) have to be involved.

This is set of rules I found:

=== Summary of package splitting for Log4J1 (deprecated information in 1.12.x and 2.1.x)

I think users deserve this summary, because there are 4 bundles/jars:

And there's this design flaw that single JAR is treated as both API and Implementation (what's worse - some packages mix API and Implementation classes).

log4j:apache-log4j-extras source JAR (and github repository) duplicates these packages from log4j:log4j:

But fortunately doesn't duplicate any of actual source files.

log4j:apache-log4j-extras JAR duplicates the above packages where the classes are simply merged from own project and from log4j:log4j JAR. However, pax-logging-api re-exports org.apache.log4j, org.apache.log4j.pattern, org.apache.log4j.spi and org.apache.log4j.xml from the log4j:log4j JAR, not from log4j:apache-log4j-extras, because some additional classes (like org.apache.log4j.DBAppender) introduce too many additional packages that have to be re-exported (because of uses clause).

Here's full list of packages and notes about how it's used in pax-logging.

org.apache.log4j:: This is the main package mixing all kinds of classes (API, Implementation, internal functionality, ...)

org.apache.log4j.chainsaw:: This package comes from log4j:log4j and is Private-Packaged in pax-logging-log4j1 without changes.

org.apache.log4j.component.*:: This package (and subpackages) comes from log4j:apache-log4j-extras and is Private-Packaged in pax-logging-log4j1 without changes.

org.apache.log4j.config:: This package comes from log4j:log4j.

org.apache.log4j.extras:: This package comes from log4j:apache-log4j-extras and is Private-Packaged in pax-logging-log4j1 without changes.

org.apache.log4j.filter:: This package comes from log4j:apache-log4j-extras and is Private-Packaged in pax-logging-log4j1. pax-logging-log4j1 contains additional classes:

org.apache.log4j.helpers:: This package is tricky. It's in uses closure of packages exported from pax-logging-api, but pax-logging-log4j1 can't import it. pax-logging-log4j1 fixes performance problems with AppenderAttachableImpl, but it can't import this package from pax-logging-api, because it can't import org.apache.log4j package and this root package contains org.apache.log4j.Appender class which is used as argument to some of AppenderAttachableImpl methods.

org.apache.log4j.jdbc:: This package comes from log4j:log4j and is Private-Packaged in pax-logging-log4j1 without changes.

org.apache.log4j.jmx:: This package comes from log4j:log4j and is Private-Packaged in pax-logging-log4j1 without changes.

org.apache.log4j.lf5.*:: This package (and subpackages) comes from log4j:log4j and is Private-Packaged in pax-logging-log4j1 without changes.

org.apache.log4j.net:: This package comes from log4j:log4j and is Private-Packaged in pax-logging-log4j1 without changes.

org.apache.log4j.nt:: This package comes from log4j:log4j and is Private-Packaged in pax-logging-log4j1 without changes.

org.apache.log4j.or (Object Renderer)::

org.apache.log4j.or.jms:: This package comes from log4j:log4j and is Private-Packaged in pax-logging-log4j1 without changes.

org.apache.log4j.or.sax:: This package comes from log4j:log4j and is Private-Packaged in pax-logging-log4j1 without changes.

org.apache.log4j.pattern:: This package comes from log4j:log4j, but log4j:apache-log4j-extras adds ExtrasFormattingInfo and ExtrasPatternParser.

org.apache.log4j.receivers.*:: This package (and subpackages) comes from log4j:apache-log4j-extras and is Private-Packaged in pax-logging-log4j1 without changes.

org.apache.log4j.rewrite:: This package comes from log4j:log4j and is Private-Packaged in pax-logging-log4j1 without changes.

org.apache.log4j.rolling.*:: This package (and subpackages) comes from log4j:apache-log4j-extras and is Private-Packaged in pax-logging-log4j1.

org.apache.log4j.rule:: This package comes from log4j:apache-log4j-extras and is Private-Packaged in pax-logging-log4j1 without changes.

org.apache.log4j.sift:: That's entirely pax-logging-log4j1 private package with MDCSiftingLoggingAppender class created for https://ops4j1.jira.com/browse/PAXLOGGING-83[PAXLOGGING-83]

org.apache.log4j.spi::

org.apache.log4j.varia:: This package comes from both log4j:log4j and log4j:apache-log4j-extras and is Private-Packaged in pax-logging-log4j1 without changes.

org.apache.log4j.xml:: This package comes from both log4j:log4j and log4j:apache-log4j-extras (which adds XSLTLayout class).

org.apache.log4j.zip:: That's entirely pax-logging-log4j1 private package with ZipRollingFileAppender class created for https://ops4j1.jira.com/browse/PAXLOGGING-116[PAXLOGGING-116]

=== Location Info

When Log4J1 is used with pattern layout that deals with class/method names and/or file names and line numbers, there's a need to analyze stack trace to get this info.

When log4J1 is called normally, without ANY facade (and outside of pax-logging), the relevant stack trace fragment is:

[listing,options=nowrap]

"main@1" prio=5 tid=0x1 nid=NA runnable java.lang.Thread.State: RUNNABLE at org.apache.log4j.spi.LocationInfo.(LocationInfo.java:144) at org.apache.log4j.spi.LoggingEvent.getLocationInformation(LoggingEvent.java:253) at org.apache.log4j.helpers.PatternParser$LocationPatternConverter.convert(PatternParser.java:500) at org.apache.log4j.helpers.PatternConverter.format(PatternConverter.java:65) at org.apache.log4j.PatternLayout.format(PatternLayout.java:506) at org.apache.log4j.WriterAppender.subAppend(WriterAppender.java:310) at org.apache.log4j.WriterAppender.append(WriterAppender.java:162) at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251) at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:66) at org.apache.log4j.Category.callAppenders(Category.java:206) at org.apache.log4j.Category.forcedLog(Category.java:391) at org.apache.log4j.Category.info(Category.java:666) at org.ops4j.pax.logging.test.log4j1.Log4j1NativeApiTest.loggerAPI(Log4j1NativeApiTest.java:80) ...

The discovered class name shuold be org.ops4j.pax.logging.test.log4j1.Log4j1NativeApiTest. What log4j ensures to make it work is passing org.apache.log4j.Category.FQCN (or org.apache.log4j.Logger.FQCN) value down through org.apache.log4j.Category.forcedLog method. Then the last stack trace element before FQCN is used to collection location info.

When Log4J1 is used through SLF4J, org.slf4j.impl.Log4jLoggerAdapter.FQCN is used to pass through org.apache.log4j.Category.log() and org.apache.log4j.Category.callAppenders().

With pax-logging, the stack trace is a bit more complex: [listing,options=nowrap]

"Karaf Shell Console Thread@9179" daemon prio=5 tid=0x31 nid=NA runnable java.lang.Thread.State: RUNNABLE at org.apache.log4j.spi.LocationInfo.(LocationInfo.java:136) at org.apache.log4j.spi.LoggingEvent.getLocationInformation(LoggingEvent.java:253) at org.apache.log4j.helpers.PatternParser$ClassNamePatternConverter.getFullyQualifiedName(PatternParser.java:555) at org.apache.log4j.helpers.PatternParser$NamedPatternConverter.convert(PatternParser.java:528) at org.apache.log4j.helpers.PatternConverter.format(PatternConverter.java:65) at org.apache.log4j.PatternLayout.format(PatternLayout.java:506) at org.apache.log4j.WriterAppender.subAppend(WriterAppender.java:310) at org.apache.log4j.RollingFileAppender.subAppend(RollingFileAppender.java:276) at org.apache.log4j.WriterAppender.append(WriterAppender.java:162) at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)

And the FQCN that's equal to org.ops4j.pax.logging.slf4j.Slf4jLogger is ensured by pax-logging-api and shaded classes from given facade (here - SLF4J).

When pax-logging is used with Log4J1 and without SLF4J, stack trace is like: [listing,options=nowrap]

"Karaf Shell Console Thread@9190" daemon prio=5 tid=0x31 nid=NA runnable java.lang.Thread.State: RUNNABLE at org.apache.log4j.spi.LocationInfo.(LocationInfo.java:136) at org.apache.log4j.spi.LoggingEvent.getLocationInformation(LoggingEvent.java:253) at org.apache.log4j.helpers.PatternParser$ClassNamePatternConverter.getFullyQualifiedName(PatternParser.java:555) at org.apache.log4j.helpers.PatternParser$NamedPatternConverter.convert(PatternParser.java:528) at org.apache.log4j.helpers.PatternConverter.format(PatternConverter.java:65) at org.apache.log4j.PatternLayout.format(PatternLayout.java:506) at org.apache.log4j.WriterAppender.subAppend(WriterAppender.java:310) at org.apache.log4j.RollingFileAppender.subAppend(RollingFileAppender.java:276) at org.apache.log4j.WriterAppender.append(WriterAppender.java:162) at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)

So the FQCN should be org.apache.log4j.Logger. Even if the logger is obtained via org.apache.log4j.Category static methods, the logger is of org.apache.log4j.Logger class and stack trace analysis works without problems. Also, trace/debug/info/warn/error/fatal methods are defined in Category class, but overriden in Logger, to properly detect the calling class/method.

But not all logging methods are overriden... [listing,options=nowrap]

"Karaf Shell Console Thread@9205" daemon prio=5 tid=0x31 nid=NA runnable java.lang.Thread.State: RUNNABLE at org.apache.log4j.spi.LocationInfo.(LocationInfo.java:136) at org.apache.log4j.spi.LoggingEvent.getLocationInformation(LoggingEvent.java:253) at org.apache.log4j.helpers.PatternParser$ClassNamePatternConverter.getFullyQualifiedName(PatternParser.java:555) at org.apache.log4j.helpers.PatternParser$NamedPatternConverter.convert(PatternParser.java:528) at org.apache.log4j.helpers.PatternConverter.format(PatternConverter.java:65) at org.apache.log4j.PatternLayout.format(PatternLayout.java:506) at org.apache.log4j.WriterAppender.subAppend(WriterAppender.java:310) at org.apache.log4j.RollingFileAppender.subAppend(RollingFileAppender.java:276) at org.apache.log4j.WriterAppender.append(WriterAppender.java:162) at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)

When calling org.apache.log4j.Category.log(org.apache.log4j.Priority, java.lang.Object) directly, the method is defined in Category class, so when analyzing stack trace, org.apache.log4j.Category.log(Category.java:858) will be detected as logging event location. This will be fixed with PAXLOGGING-252.

The location info should be org.ops4j.pax.logging.test.OsgiLogServiceApiTest.logServiceAPI(). FQCN is ... "" location can't be found and in logs we can see (for pattern %d{ISO8601} | %-5.5p | {%t} [%c]/[%C] (%F:%L) | %m%n and symbolic name = my-bundle): [listing,options=nowrap]

2019-04-26 08:11:53,126 | INFO | {Karaf Shell Console Thread} [my-bundle]/[?] (?:?) | Hello!

=== API / Implementation separation

The biggest problem with Log4J1 is not only OSGi-specific problem of having API and implementation classes in single log4j:log4j library. Even methods are mixed within single class.

org.apache.log4j.Logger (together with its superclass org.apache.log4j.Category) class contains ~80 methods. These methods can be groupped into:

The above groupping is much better implemented in other logging frameworks which have separate logger and factory classes and also do the configuration and all the meta in different way (than through single logger class).

== Logback

As mentioned on https://logback.qos.ch/[project's web page], Logback picks up where log4j leaves off.

Logback was created after the logging-bridge (r)evolution and even if it may be used without any logging facade/bridge, it is very uncommon to do so. That's why there are no special API classes in pax-logging-api related to Logback. Logback is handled by pax-logging only through implementation of org.ops4j.pax.logging.PaxLoggingService.

Logback is mostly used behind SLF4J facade and both logger factory and MDC/NDC API comes from SLF4J itself when dealing with Logback.

Logback is initialized using org.slf4j.impl.StaticLoggerBinder Slf4J mechanism - but only if such class is explicitly requested/loaded (e.g., through org.slf4j.LoggerFactory.getLogger() and org.slf4j.LoggerFactory.bind()). With pax-logging-logback, Logback's version of org.slf4j.impl.StaticLoggerBinder is neither exported nor used.

pax-logging-logback implementation of org.ops4j.pax.logging.PaxLoggingService explicitly configures ch.qos.logback.classic.LoggerContext instance (which, by the way, implements org.slf4j.ILoggerFactory).

=== Logback contrib

See https://github.com/qos-ch/logback-contrib

There are several additional JARs we Private-Package in pax-logging-logback:

After private-packaging the above, I've adjusted the generated Import-Package header providing explicit version ranges for Groovy and Jackson and making some imports optional.

== Log4J2

After huge (in my humble, subjective opinion) success of Logback, Log4J2 was created as modernized version of original Log4j project with full awareness of logging bridges/facades and weird properties file syntax.

pax-logging provides dedicated implementation of org.ops4j.pax.logging.PaxLoggingService that delegates to Log4J2.

Again, Log4J2 itself may be used without bridge/facade and (differently than with Logback) pax-logging fully supports its native API.

Here's a list of all org.apache.logging.log4j artifacts I found in version 2.11.2:

Currently, pax-logging uses 3:

I'm going to include some more just like with log4j:apache-log4j-extras and ch.qos.logback.contrib.

These won't be supported/embedded/referenced:

The remaining Log4J2 artifacts can be split into 3 categories:

The original exports of org.apache.logging.log4j:log4j-api are:

[listing,options=nowrap]

org.apache.logging.log4j; version="2.11.2"; uses:="org.apache.logging.log4j.message, org.apache.logging.log4j.spi, org.apache.logging.log4j.util" org.apache.logging.log4j.message; version="2.11.2"; uses:="org.apache.logging.log4j.util" org.apache.logging.log4j.simple; version="2.11.2"; uses:="org.apache.logging.log4j,org.apache.logging.log4j.message,org.apache.logging.log4j.spi,org.apache.logging.log4j.util" org.apache.logging.log4j.spi; version="2.11.2"; uses:="org.apache.logging.log4j,org.apache.logging.log4j.message,org.apache.logging.log4j.util" org.apache.logging.log4j.status; version="2.11.2"; uses:="org.apache.logging.log4j,org.apache.logging.log4j.message,org.apache.logging.log4j.spi" org.apache.logging.log4j.util; version="2.11.2"; uses:="org.apache.logging.log4j.message,org.apache.logging.log4j.spi,org.osgi.framework"

This perfectly matches what pax-logging-api (re)exported. These are actually all the packages included in org.apache.logging.log4j:log4j-api.

=== Plugins

Log4J2 is extended using plugin system. Quoting http://lo[the manual]:

==== In Log4j 2 a plugin is declared by adding a @Plugin annotation to the class declaration. During initialization the Configuration will invoke the PluginManager to load the built-in Log4j plugins as well as any custom plugins. The PluginManager locates plugins by looking in five places:

  1. Serialized plugin listing files on the classpath. These files are generated automatically during the build (more details below).
  2. (OSGi only) Serialized plugin listing files in each active OSGi bundle. A BundleListener is added on activation to continue checking new bundles after log4j-core has started.
  3. A comma-separated list of packages specified by the log4j.plugin.packages system property.
  4. Packages passed to the static PluginManager.addPackages method (before Log4j configuration occurs).
  5. The packages declared in your log4j2 configuration file.

Currently, pax-logging doesn't do the same discovery as bundle activator of original org.apache.logging.log4j:log4j-core. Though similar mechanism may be added in the future.

org.apache.logging.log4j.core.config.plugins.util.PluginManager.collectPlugins() collects the plugins from different sources. The cache file is declared as org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor.PLUGIN_CACHE_FILE and refers to META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat. It's a binary file conforming to java.io.DataInputStream which may occur multiple times on the classpath.

pax-logging-log4j2 bundle directly uses the original plugin cache file from org.apache.logging.log4j:log4j-core and additional plugins are added using org.apache.logging.log4j.core.config.plugins.util.PluginManager.addPackage() during pax-logging-log4j2 initialization.

Default cache file contains exactly these categories and numbers of plugins (206 total): [listing,options=nowrap]

cache = {org.apache.logging.log4j.core.config.plugins.processor.PluginCache@1030} categories: java.util.Map = {java.util.LinkedHashMap@1059} size = 6 "core" -> {java.util.LinkedHashMap@1069} size = 117 "converter" -> {java.util.LinkedHashMap@1071} size = 44 "lookup" -> {java.util.LinkedHashMap@1073} size = 13 "configurationfactory" -> {java.util.LinkedHashMap@1075} size = 4 "fileconverter" -> {java.util.LinkedHashMap@1077} size = 2 "typeconverter" -> {java.util.LinkedHashMap@1079} size = 26

=== Configuration

Log4J2 has complex configuration mechanisms and can process configuration from different sources. Configuration may be stored in XML, JSON, YAML and properties files. Among these, properties file (very common in Log4J1 times) is the most confusing...

org.apache.logging.log4j.core.config.builder.impl.DefaultConfigurationBuilder is the most important class here that allows to understand how configuration is organized. This builder includes org.apache.logging.log4j.core.config.builder.api.Component components for these concepts:

org.apache.logging.log4j.core.config.builder.api.Component is generally a container for:

Plugin types of components inside "root" component may be one of:

But generally, plugin type is a key for org.apache.logging.log4j.core.config.plugins.util.PluginManager.plugins map which maps names to org.apache.logging.log4j.core.config.plugins.util.PluginType

== Testing in Karaf

That's tricky problem. If we want to use Pax Exam and test Pax Logging under Karaf with Maven we have to consider:

Then, taking into account the logging process itself:

Summarizing (for pax-logging-it-karaf/karaf-it):