Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing log4j 1.x and log4j 2

I have a new application that is being written using log4j2, to take advantage of some of its new features. One of the libraries it uses is a bit older and was built with log4j 1.x. I'm having a problem where the new code in the application writes to the log, but the code in the old library does not write to the log. I even have one class which is a subclass of something in the old library and is also called by code in the old library, and this code also will not write to the log. I have configured the application to use log4j2 version 2.0.2, and I explicitly excluded log4j when referencing the old library as a maven dependency.

How can I get the code in the old library logging into the same file as my new code?

Re-writing the old library should be thought of as a last-resort option

like image 749
FrustratedWithFormsDesigner Avatar asked Sep 23 '14 21:09

FrustratedWithFormsDesigner


People also ask

Can we use Log4j and log4j2 together?

Log4j 2 provides support for the Log4j 1 logging methods by providing alternate implementations of the classes containing those methods. These classes may be found in the log4j-1.2-api jar distributed with the project.

Is Log4j 2 backwards compatible?

Log4j 2 is not backwards compatible with 1.

What is the difference between Log4j 1 x and 2x?

Community support: Log4j 1. x is not actively maintained, whereas Log4j 2 has an active community where questions are answered, features are added and bugs are fixed. Automatically reload its configuration upon modification without losing log events while reconfiguring.


1 Answers

Are you aware that log4j2 includes a bridge for log4j-1.2? You can use it by removing the old log4j-1.2.17.jar, and include these three jars:

  • log4j-api-2.x.jar
  • log4j-core-2.x.jar
  • log4j-1.2-api-2.x.jar

This will result in all calls that your application makes to the log4-1.2 API to be routed to the log4j2 implementation. The FAQ has a diagram that may clarify things. (Don't forget to remove the old log4j-1.2 jar from the classpath!)

like image 76
Remko Popma Avatar answered Sep 22 '22 00:09

Remko Popma