Log4j multiple WAR files in single EAR configuration

Log4j multiple WAR files in single EAR configuration


Problem:


When you have multiple WAR files under the same EAR with multiple log4j config files in each of the war files, the logging API will pickup the first available config file and ignores the list. i.e, only a single log4j config file applies to entire EAR.

Consider the below app

app.ear
|__admin.war
   |__WEB-INF/classes/log4.xml
|__web.war
   |__WEB-INF/classes/log4j.xml

When you deploy this app to the server all logs of two war files admin.war and web.war will go to admin.war' log. The log4j.xml in web.war has no effect here.


There are some complex solutions for this like adding a logback, specifying package level log files in log4j configuration etc.

Solution


The easiest way is configuring the context param in web.xml

In your web.xml add the following context param will send the log4j config locations specific to the contexts and fixes this issue.

web.war/WEB-INF/classes/log4j.xml
<context-param>

      <param-name>log4jContextName</param-name>

      <param-value>web</param-value> <!-- here web is the context root for web.war -->

 </context-param>


The complete log4j configuration in web.xml for web.war will look something like below.

<!-- here spring listener is used, anything specific to frameworks in your app holds good -->

<listener>

    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

</listener>



<context-param>

   <param-name>log4jConfigLocation</param-name>

   <param-value>classpath:/main/resources/log4j.xml</param-value>

</context-param>



<context-param>

  <param-name>log4jContextName</param-name>

  <param-value>web</param-value>

</context-param>

Comments

  1. It didnt work. After adding above solution all logging starts going inside web.war log file - Naveen

    ReplyDelete
  2. This post is so interactive and informative.keep updating more information...
    ETL tools
    Python

    ReplyDelete
  3. valuable blog,Informative content...thanks for sharing, Waiting for the next update…
    How Does Flutter Work
    What is google flutter?

    ReplyDelete
  4. valuable blog,Informative content...thanks for sharing, Waiting for the next update...
    Different Types of Protocols
    Different types of network protocols

    ReplyDelete

Post a Comment

Popular posts from this blog

Ubuntu / kubuntu - Laptop key board cursor jumping issue

Java NIO2 - Watching a directory for changes. FileWatcherService.