Search code examples
javajbosslog4jwebsphereejb

Log4j version 2 not logging


I'm updating log4j to version 2.17.1 for my application (it's not actually mine, but it is for my company's project, so i must follow all my company standard).

To configure log4j we use a log4j2.xml stored on server file system.

My application is .ear EE application which is structured this way:

  • libs folder containg all externals libraries
  • META-INF folder containing application descriptors
  • myapp.jar containing ejb modules
  • myapp.web containing web app descriptors

We configure log4j2 by code passing the path of log42.xml file located in app server file system:

LoggerContext context = (LoggerContext) LogManager.getContext(false);                  
context.setConfigLocation(new File(log4jConfFileName).toURI()); //the path where log42.xml is stored in app server file system                   
context.updateLoggers();                                                              

This is the log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="30">
    <Appenders>
        <RollingFile name="main_file_appender"
            filePattern="${sys:logfile.path}/myapp/myapp.%d{yyyy-MM-dd-HH}.log">
            <PatternLayout
                pattern="%d{DEFAULT} - %-5level [%c{1}] %X{clientId}- %msg%n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
            </Policies>
        </RollingFile>
    </Appenders>

    <Loggers>
        <Root level="off" />
        <Logger name="com.mycomp.myapp" level="debug">
            <AppenderRef ref="main_file_appender" />
        </Logger>
        <Logger name="com.mycomp.devicehandlers" level="debug">
            <AppenderRef ref="main_file_appender" />
        </Logger>
        <Logger name="com.mycomp.switchcommons" level="trace">
            <AppenderRef ref="main_file_appender" />
        </Logger>
        <Logger name="com.mycomp.basewebapp" level="debug">
            <AppenderRef ref="main_file_appender" />
        </Logger>
    </Loggers>
</Configuration>

The line of java code previously showed it is written in a servlet stored in a library under /libs folder of my application. When the servlet starts, it loads the log4j2.xml through that java code shown before

When I deploy this application in WebSphere application server, all the loggers work.

The problem occurs when I deploy my application into JBoss application server (version 7.1): the application is able to log all the stuff contained in the classes of all libraries under "/libs" folder, but it is not able to log the stuff of classes contained in myapp.jar (as shown before).

So the classes under "com.mycomp.myapp" does not log anything because they are located in myapp.jar, while the classes under "com.mycomp.devicehandlers", "com.mycomp.switchcommons" and "com.mycomp.basewebapp" can log everything because those packages are stored in libraries under /libs folder of my .ear EE application (in fact library names are respectively devicehandlers.jar, switchcommons.jar and basewebapp.jar. basewebapp.jar contains the servlet that initialize the logging context with that java code shown before).

I tried all solutions possible, but nothing works. I tried to change the jboss-deployment-structure.xml in all the ways possible. There's no difference if I try to change the logger name from "com.mycomp.myapp" into any string. I cant't change the struct of the ear application so libs must stay in /libs folder and myapp.jar must stay where it is. I tried to use different ways to initialize the java context like this:

Configurator.initialize(servletContext.getServletContextName(), null, log4jConfFileName);

or this:

Configurator.initialize(null, log4jConfFileName);

This is application.xml under META-INF

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" version="6">
  <display-name>myapp 1.23.5-RC</display-name>
  <module>
    <web>
      <web-uri>myapp.war</web-uri>
      <context-root>/myapp</context-root>
    </web>
  </module>
  <module>
    <ejb>myapp.jar</ejb>
  </module>
  <library-directory>libs</library-directory>
</application>

How can I make the classes under "com.mycomp.myapp" to log in JBoss 7.1? Did anyone experience the same problem?


Solution

  • We have fixed the problem. The problem was caused by LogManager.getLogget log4j2 method. We are using log4j 1-2 bridge library and the old Logger.getLogger method.