Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring log4j2 and log4j using a single log4j2 xml file

I've migrated my application to log4j 2, and I've configured it via log4j2.xml

However, some of the libraries I'm using depend on log4j 1. If I run the application using:

-Dlog4j.configurationFile=path/to/log4j2.xml 

log4j 1 complains about not finding a configuration file. I'm using the log4j 1.x bridge provided by log4j 2, log4j-1.2-api-2.0-rc1.jar. Is it possible to configure both using a single log4j2.xml?

An alternative I've tried is configuring both log4j and log4j2 together:

-Dlog4j.configurationFile=path/to/log4j2.xml -Dlog4j.configuration=path/to/log4j.xml 

My concern is fragmentation of my logging configuration files and output. I'm also concerned about possible conflicts between log4j.xml and log4j2.xml. e.g. the logfile error.log is configured to use a FileAppender in log4j 1 and a RollingFileAppender in log4j 2.

Any advice?

[note]

This is the error I'm seeing:

log4j:WARN No appenders could be found for logger (org.apache.activemq.util.ThreadPoolUtils). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 

The version of log4j 2 I'm using is log4j 2.0 rc1.

[answer]

Seems like activemq-5.8.0.jar was bundled with log4j 1. The solution was simply to load the log4j 1.x bridge before activemq.

like image 778
Justin Wong Avatar asked Mar 11 '14 09:03

Justin Wong


People also ask

Can we use both log4j and log4j2?

@kensai Yes, you add the log4j2 dependency but in the dependency that should use log4j (and not log4j2), you add the exclusion. so these exclusions should appear for log4j-1.2-api-2.6. 1.

Where do I put my log4j2 XML file?

We should put log4j2. xml anywhere in the application's classpath. Log4j will scan all classpath locations to find out this file and then load it. We can find this file mostly placed in the 'src/main/resources' folder.

What is configuration status in log4j2 xml?

Configuration: the root element of a log4j2 configuration file; the status attribute represents the level at which internal log4j events should be logged. Appenders: this element contains a list of appenders; in our example, an appender corresponding to the System console is defined.


1 Answers

I would recommend using the log4j-1.2 adapter that is included in the log4j2 distribution. That way, any libraries coded to the log4j-1.2 API will work with log4j2 without any code changes.

Your classpath should include:

  • log4j-api-2.6.1.jar
  • log4j-core-2.6.1.jar
  • log4j-1.2-api-2.6.1.jar
  • log4j2.xml

Your classpath should not include:

  • log4j-1.2.x.jar
  • log4j.properties or log4j.xml (these will be ignored by log4j2 anyway)

See also http://logging.apache.org/log4j/2.x/faq.html#which_jars

like image 170
Remko Popma Avatar answered Sep 21 '22 09:09

Remko Popma