2
votes

We have following Set up:

Application Server : Weblogic 10.3.6

We have 2 Managed Servers and we have 3 web applications wars deployed on both the managed servers. All log4j2 jars are kept in server lib and loaded on server start up.

Now we want each web application to have its own logj2 configuration as we want to have control on logs levels.

we made separate log4j2 configuration file for each web application using tried initialising same using log4j2-web.jar in web.xml but we observed that when we load first web application, the log4j2 configuration file get loaded but when we try to load 2nd and 3rd web application,with their separate log4j config xmls,it doesnt load the new config file but sets log4j2 configurations of first web applications which got deployed first.

I tried to debug same and got to the point that as log4j2 jars are present in server class loader so they are common to all web application and log4j2 uses class loader to load logger context.

Now i want each application to have its separate log4j2 implementation which will not be common to other web application.so logging will be different for each application.

Kindly note, i cannot copy log4j2 libraries in WEB-INF/lib of each web applications as we already have size constrain for war.

Kindly suggest how can i implement separate log4j2 configuration for each web application in single weblogic container.

I tried to use JNDIContextSelector but its not working in Weblogic or i dont know how to make it work in weblogic as after doing all the required changes mentioned in log4j2 website,it was not able to find loggers.

Kindly help as i m stuck in same issue from last week.

1

1 Answers

1
votes

Unfortunately by far the easiest solution that I am aware of is to include the log4j2 libraries in WEB-INF/lib of each web application...

If the log4j2 jar files are in the shared library directory, they are loaded by the shared classloader, and the configuration is shared between all web applications. This is how web containers and log4j2 are designed...

I would focus on this size constraint you mention. Why is there a size constraint on your war file? Perhaps there is another way to get around this size constraint? Would buying extra hard disk or a bigger server solve the issue? (Hardware is much much cheaper than the time you and perhaps your dev team are spending on this...)

If you are really desperate you can try to have one configuration with differently named loggers that log to different files or filter on different levels. When your application calls LogManager.getLogger, the string you pass becomes the name of the logger. This string can be used in the configuration to control filtering and the target appender. One thing you can do in your application, is giving loggers application-unique names like this:

// App.getPrefix() returns "app1." or "app2." etc: a unique name for each web app
Logger logger = LogManager.getLogger(App.getPrefix() + getClass().getName());

Then have different loggers and appenders in your configuration:

<Configuration status="debug">
  <Appenders>
     <File name="app1File" ... 
     <File name="app2File" ... 
  </Appenders>
  <Loggers>
    <Logger name="app1" level="TRACE">
      <AppenderRef ref="app1File"/>
    </Logger>
    <Logger name="app2" level="TRACE">
      <AppenderRef ref="app2File"/>
    </Logger>
  </Loggers>
</Configuration>

This is obviously a bit of a hack, and won't work for the classes outside your dev team's control...

You can also ask on the log4j-user mailing list to see if any of the other log4j2 team members have any ideas.