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. IEEE Final Year Project centers make amazing deep learning final year projects ideas for final year students Final Year Projects for CSE to training and develop their deep learning experience and talents.

    IEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation.

    corporate training in chennai corporate training in chennai

    corporate training companies in india corporate training companies in india

    corporate training companies in chennai corporate training companies in chennai

    I have read your blog its very attractive and impressive. I like it your blog. Digital Marketing Company in Chennai

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

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

    ReplyDelete
  5. 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

SVN File Creation Date