3

I have successfully migrated our application from log4j 1.x to log4j 2.x. But there is one external component which cannot be migrated. Meaning calls to log4j 1.x should remain as they are.

I have log4j2.xml for our application and the old log4j.properties for the external component.I am getting the logs for the appenders in log4j2.xml but nothing for the one defined in log4j.properties. I read the post - Mixing log4j 1.x and log4j 2

and have added the required jars. Earlier with log4j 1.x we used to have 2 log4j.properties - one for the applciation and another one for the external component using PropertyConfigurator. Now with log4j2.xml I am not sure how we can achieve this.

Configuring log4j2 and log4j using a single log4j2 xml file This post says we cannot have log4j.properties in the classpath. Does that mean that I need to transfer all the appenders defined in log4j.properties to log4j2.xml ? Can I have a seperate log4j2.xml just for the external component.

Community
  • 1
  • 1
flume
  • 115
  • 1
  • 3
  • 6

1 Answers1

2

You cannot have two log4j2.xml configurations (well, separate web apps can have separate configurations but in general each process has one config).

Option 1: The easiest thing to do is to use the bridge log4j-1.2-api-2.x.jar and merge the log4j.properties configuration into the log4j2.xml. Then all logging is done by lo4j 2 and it should all work.

If you want to do things the hard way you may be able to succeed but it may take some fiddling.

Option 2: If by "migrated our application from log4j 1.x to log4j 2.x" you mean your application now uses the log4j 2 API, then you should be able to use both log4j 1.x to log4j 2 at the same time. (log4j 1.x for the external component.)

In that case, do not use the bridge log4j-1.2-api-2.x.jar. If you use the bridge, it will route calls to the log4j 1.x API to log4j 2. Usually that is what you want, but in this case it sounds like your external component wants to use the log4j 1.x internals. (Is this true? If not, consider option 1.)

If your application now uses the log4j 2 API, except for the external component, then put these jars and config files in your classpath:

  • log4j-api-2.x.jar
  • log4j-core-2.x.jar
  • log4j-1.2.x.jar
  • log4j2.xml - the log4j 2 configuration for your application
  • log4j.properties - the log4j 1.x configuration for the external component

But again, simply merging the configurations is probably easier to maintain.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114