27

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.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
Justin Wong
  • 1,455
  • 3
  • 18
  • 25

2 Answers2

40

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

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • I'm already using log4j-1.2-api-2.0.jar in my classpath. There is no log4j-1.2.x.jar in my classpath at all. I was using the method you recommended above, but log4j still complains about not being configured properly. Sorry if I wasn't clear in my initial question, I'll update to reflect this. – Justin Wong Mar 11 '14 at 09:47
  • If there is no log4j-1.2.x.jar in the classpath, then how can log4j-1.2 still complain (the classes doing the complaining should not be on the classpath)? – Remko Popma Mar 11 '14 at 09:52
  • I believe it's complaining due to the log4j 1.x bridge. – Justin Wong Mar 11 '14 at 09:53
  • Log4j2 should find the log4j2.xml config file with the system property you mentioned, but the error indicates that this is not the case... Have you tried putting log4j2.xml in the classpath without specifying a location with `-Dlog4j.configurationFile`? – Remko Popma Mar 11 '14 at 09:57
  • The log4j2.xml is in a custom location. I have verified that the application can find log4j2.xml as my own application code which uses log4j2 follows the configuration I have specified in log4j2.xml. – Justin Wong Mar 11 '14 at 10:00
  • After re-reading your description, I am convinced that the warning messages you are seeing (`log4j:WARN ...`) indicate that the log4j-1.2.x.jar file is still on the classpath somewhere. – Remko Popma Mar 11 '14 at 10:01
  • This will show you where it is: `System.out.println(org.apache.log4j.spi.Configurator.class.getResource("/org/apache/log4j/spi/Configurator.class"));` – Remko Popma Mar 11 '14 at 10:09
  • Thanks for the advice! After further investigation I have found log4j 1 embedded into the activemq jar file which is complaining about log4j. I believe I can deal with this by loading log4j-1.2-api-2.0.jar before activemq. Will experiment and update my question with my results. – Justin Wong Mar 11 '14 at 10:09
  • Embedded in the activemq jar?! I think you just proved that that's a bad idea. You should complain to the activemq people! – Remko Popma Mar 11 '14 at 10:11
  • 1
    Actually its really my fault for using the wrong jar file, I just discovered that too. Many thanks for the advice though =) – Justin Wong Mar 11 '14 at 10:16
  • @Justin - how did you resolve the activemq issue? which jar is the correct one? – Viorel Florian Jan 27 '17 at 09:28
  • 2
    @Viorel I was using activemq-all, when I really should have been using the individual libraries as required i.e. activemq-client, activemq-core, activemq-broker, etc – Justin Wong Mar 05 '17 at 03:42
4

In a maven project using log4j2, it is possible to exclude log4j from modules that use it, in the pom, in order to enable log4j2 to take over:

<dependencies>
    <dependency>
        <groupId>commons-configuration</groupId>
        <artifactId>commons-configuration</artifactId>
        <version>1.4</version>
        <!--force usage of log4j2-->
        <exclusions>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-1.2-api</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-web</artifactId>
        <version>2.3</version>
    </dependency>
</dependencies>

More info about that in the log4j FAQ

OOP
  • 293
  • 4
  • 16
  • So you will leave it in listed, but at same time you define it in and it will work that way? – kensai Apr 23 '17 at 12:54
  • @kensai Yes, you add the log4j2 dependency but in the dependency that should use log4j (and not log4j2), you add the exclusion. – OOP Apr 24 '17 at 09:33
  • so these exclusions should appear for log4j-1.2-api-2.6.1.jar artefact? Or which one is it meant for? – kensai Apr 24 '17 at 10:41
  • I have actually platform which by default uses only log4j (no native log4j2 support) and I try to set it up for log4j routing to log4j2 and trying to setup properly the maven pom.xml. With accepted answer it works, the remaining question is where to put exclusions mentioned by you. Thanks a lot. – kensai Apr 24 '17 at 10:50