Search code examples
javaloggingjbosslog4j2

Log4j2 with Jboss does not work


I got a problem with log4j2 and I'd like to understand what is the issue. I used to log with log4j 1.2 on a webapp based on jsf and spring, and running on JBoss 7.1.2, and I am now trying to replace it with log4j 2.8, but it seems not to work. I cannot understand which is the problem, since from server logs it seems to find the log4j2.xml when it has problems, but it says nothing when it has got no syntax errors, but it also stop printing in console, so I think both it is finding that config file and not doing anything else. Maybe is better i show you the code instead of talking about it. I can't guess what is wrong, i followed apache instructions, many other examples, but still does not work.
pom.xml:

  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.8.1</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.8.1</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-jul</artifactId>
    <version>2.8.1</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-web</artifactId>
    <version>2.8.1</version>
  </dependency>

jboss-deployment-structure.xml

<exclusions>
[...]
        <module name="org.apache.log4j" />
        <module name="org.slf4j" />
        <module name="org.apache.commons.logging"/>
        <module name="org.log4j"/>  
        <module name="org.jboss.logging"/>
        <module name='org.slf4j.impl' />
</exclusions>

web.xml:

    <context-param>
        <param-name>log4jConfiguration</param-name>
        <param-value>/WEB-INF/log4j2.xml</param-value>
    </context-param>

and also

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

and log4j2.xml under WEB-INF (it does find it since if I do syntax error it gives parsing error messages in console starting server)

 <?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Properties>
        <Property name="layoutPattern">%d %-5p %c{1}:%L %m %n</Property>
  </Properties>
  <Appenders>
    <File name="mainFileAppender" fileName="${webapp.root}/log/main.log">
        <PatternLayout>
            <Pattern>%d %d{Z} [%t] %-5p (%F:%L) - %m%n </Pattern>
        </PatternLayout>        
        </File>
    <File name="hibernateAppender" fileName="${webapp.root}/log/hibernate.log">
        <PatternLayout>
            <Pattern>%d %d{Z} [%t] %-5p (%F:%L) - %m%n </Pattern>
        </PatternLayout>
    </File>
  </Appenders>
<Loggers>
    <Logger name="org.hibernate.SQL" level="debug" includeLocation="true">
      <AppenderRef ref="hibernateAppender"/>
      <AppenderRef ref="mainFileAppender"/>
    </Logger>
    <Logger name="org.hibernate.type" level="trace" includeLocation="true">
      <AppenderRef ref="hibernateAppender"/>
    </Logger>
    <Logger name="com.example" level="trace">
        <AppenderRef ref="mainFileAppender"/>
    </Logger>
    <Root level="trace" includeLocation="true">
      <AppenderRef ref="mainFileAppender"/>
    </Root>
</Loggers>
</Configuration>

and, in an example class loaded when I enter my web app

public org.apache.logging.log4j.Logger logger = org.apache.logging.log4j.LogManager.getRootLogger();


and inside the first called method

logger.debug("Hi there");

I also tried instantiating a normal logger with LogManager.getLogger()

Anyway I at least expected to find the log directory, but it is not being created. Can You please help me? It would really be appreciated. Thank You.

Edit: Half solved; I cannot pass ${webapp.root} nor ${sys:webapp.root}, anyway I was interested in using jboss.server.log.dir variable and i solved using ${sys:jboss.server.log.dir} Now the problem is that is not logging hibernate queries as log4j1.2 did, but I'm looking at configuration to see what is wrong, since it is able to log the logger when instantiated but nothing else.


Solution

  • I tested your log4j2.xml file in a simple Dynamic Web Project using maven. I placed the log4j2.xml under the src/main/resources and created the main.log under C:\log4j2\logs. The log is created! Your ${web.root) variable is the problem. I could send you my test project if you want.