Log4j bridge
As of version 0.7, logback ships with a new module called log4j-bridge. It allows log4j users to use logback without changing a single line of code in their application. All that is needed to do is to replace the log4j.jar file with the appropriate logback jars.
How does it work?
The log4j-bridge module contains replacements of most widely
used log4j classes, namely Category
,
Level
, Logger
, MDC
,
Priority
, BasicConfigurator
and Log4jLoggerFactory
. These
replacement classes redirect loggging calls to the corresponding
logback methods.
To use log4j-bridge in your own application, the first step is to locate and remove the log4j.jar file and replace it with log4j-bridge.jar which ships with logback. Note that you still need logback-classic and its dependencies for the log4j-bridge to work properly. In summary, here is a list of the required jars:
- log4j-bridge-VERSION.jar
- logback-classic-VERSION.jar
- logback-core-VERSION.jar
- slf4j-api-VERSION.jar
This is what it takes to make logback your logging implementation when using log4j. It will use logback's automatic basic configuration, displaying the logging requests in the console. More advanced use will require a configuration file and other jars which are logback dependencies. A file called logback.xml, placed in the application's classpath, will be automatically loaded by logback.
When does it not work?
The log4-bridge module does not work when the application calls
log4j components that are not present in the bridge.
For examples, direct creation of log4j Appenders
or
Filters
will not work.
However, in most situations, log4j finds its configuration file and configures itself. In these cases, the application will only issue calls to the classes that are contained in the log4j-bridge.