0

I've just switched from Tomcat to JBoss and my 1st big problem is that I can't prepare my own log4j.xml file in web application deployed on JBoss. It is not loaded during application starts. I have logging but it is from default JBoss logging configuration

I found that in the newest JBoss 7.2.0 log4j should work out of the box. But it doesn't for me. I tried all the tricks/hints/hacks I had googled - and it is so frustrated.

I would like to have log4j.xml somewhere in my project. Ideally I don't want to modify configuration inside jboss server directory because it has to work - not only on my local, but in other environments too (e.g. openshift).

pom.xml:

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.7.5</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
  <scope>provided</scope>
</dependency>

Structure:

WEB-INF/classes/log4j.xml - Contains rubbish data to check if JBoss read it (should throw exception once it load this file). I tried also copying this file into /resources, /, WEB-INF.

WEB-INF/jboss-deployment-structure.xml

<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.0">
  <deployment>
    <dependencies>
      <module name="org.slf4j"/>
      <module name="org.apache.log4j" />
    </dependencies>
  </deployment>
</jboss-deployment-structure>

Do I need to have external loader which load my log4j file?

Update #1 WEB-INF/classes/log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/' debug="true">
  <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
    </layout>
  </appender>
  <root>
    <priority value ="debug" />
    <appender-ref ref="STDOUT" />
  </root>
</log4j:configuration>

Somewhere in my code:

private static final Logger log = LoggerFactory.getLogger(TaskResource.class);
...
log.debug("deubg");
lukastymo
  • 26,145
  • 14
  • 53
  • 66
  • look here http://stackoverflow.com/questions/14457652/can-you-explain-step-by-step-how-to-use-log4j-in-ejb-module-in-jboss7-1 it makes no sense to use it with xml file for single application at a time when Jboss comes allowing you to configure it in its configuration file.. – LMG Sep 29 '13 at 13:14

3 Answers3

3

I don't know why this was so hard in JBoss, but finally I figure out all configuration. (I was very close from the beginning).

To have own log4j.xml in JBoss application you have to follow these steps:

  • put log4j.xml in WEB-INF/classes
  • exclude jboss log4j like that:

WEB-INF/jboss-deployment-structure.xml:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
  <deployment>
    <exclusions>
      <module name="org.apache.log4j" />
    </exclusions>
  </deployment>
</jboss-deployment-structure>
  • copy log4j.jar into WEB-INF/lib
  • set dependency as provided

pom.xml:

<dependency>
  <artifactId>log4j</artifactId>
  <groupId>log4j</groupId>
  <version>1.2.17</version>
  <scope>provided</scope>
</dependency>

If you need to have logs in console, set this:

-Dorg.jboss.as.logging.per-deployment=false
lukastymo
  • 26,145
  • 14
  • 53
  • 66
1

Try adding the following in WEB-INF/jboss-deployment-structure.xml file:

<?xml version="1.0" encoding="UTF-8"?> 
<jboss-deployment-structure>
   <deployment>
     <exclusions>
       <module name="org.apache.log4j" />
     </exclusions>
   </deployment>
 </jboss-deployment-structure> 
Madhura
  • 551
  • 5
  • 18
0

No, you just need to have your log4j.xml in the app CLASSPATH.

Contains rubbish data to check if JBoss read it

I don't understand this. Are you saying that you tried putting a working XML in the CLASSPATH and it didn't work, so you thought a rubbish version would be better?

Put the real thing in WEB-INF/classes and see if it works.

You don't need a separate loader. You're doing something else wrong. It's just logging; it's not this hard.

Maybe this can help:

https://community.jboss.org/thread/171813?start=0&tstart=0&_sscc=t

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • exactly. If that file would be read by JBoss I could have quick info about it. I tried like you advised before but simple, I don't have my log4j.xml read by JBoss – lukastymo Sep 29 '13 at 13:21