cool one
cool one

Reputation: 31

Application-level logs using log4j for multiple application is not working on weblogic

I have two applications deployed on server using weblogic. I have created separate properties files and configuration servlet for each application. But the problem occurring is that the logger appends to the log file of the application which is deployed latest. The code working fine on tomcat but when deployed on weblogic server it behaves like only one instance is working and that too is latest one.

log4j.properties file that i put in WEB-INF:

log4j.rootLogger=INFO, R 
log4j.appender.R=org.apache.log4j.RollingFileAppender 
log4j.appender.R.File=C:abc.log
log4j.appender.R.MaxFileSize=10MB 
log4j.appender.R.MaxBackupIndex=10 
log4j.appender.R.Append=true
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d %-5p %c (%F:%L) - %m%n

I had put the log4j-1.2.14.jar in lib folder where all the libraries are residing.

Inititalising it in servlet init() method:

public void init() throws ServletException {
    logger.info("Servlet...................");
    super.init();
    String prefix = getServletContext().getRealPath("/");
    String log4j = getServletContext().getInitParameter"log4jConfig");
    if (log4j != null) {
        PropertyConfigurator.configure(prefix + log4j);
        }
    }

and web.xml entry is:

<context-param>
       <param-name>log4jConfig</param-name>
       <param-value>/WEB-INF/log4j.properties</param-value>
  </context-param>

Upvotes: 3

Views: 3854

Answers (1)

Nulldevice
Nulldevice

Reputation: 4006

You should understand a structure of class loaders:

        Bootstrap
            |
         System
            |
         Common
         /     \
     Webapp1   Webapp2

Here are hierarchies of class loaders explained:

If you will put your log4j.jar to Common classloader, for instance, to $CATALINA_BASE/lib for Tomcat, all web applications will share the same class, and you will be able to have only one log4j configuration for all web applications.

The easiest way to solve your problem is to put separate copies of log4j.jar to WEB-INF/lib folders and log4j.properties files to WEB-INF/classes folders of your web applications. Note that in this case you will not need any log4j initialization at all. Here is a documentation on configuration of log4j for Tomcat.

UPDATED As said in Weblogic, class loading model slightly differs from Tomcat:

The weblogic.xml Web application deployment descriptor contains a <prefer-web-inf-classes> element (a sub-element of the <container-descriptor> element). By default, this element is set to False. Setting this element to True subverts the classloader delegation model so that class definitions from the Web application are loaded in preference to class definitions in higher-level classloaders. This allows a Web application to use its own version of a third-party class, which might also be part of WebLogic Server.

Please try to set <prefer-web-inf-classes> to true in Weblogic. After that, each web application probably will get it's own version of class.

Upvotes: 3

Related Questions