Configuring log4j with jboss-as-7.1.1

14,667

Solution 1

I was able to configure log4j with jboss. It turns out that you need to add exculsions seperately for each of the sub-deployments inside your main deployment. For example, I have an ear that has jar and war files budled inside. So I added seperate entries for each of the them in the jboss-deployment-structure.xml and it worked.

<sub-deployment name="your-subdeployment.jar">
    <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"/>
    </exclusions>
</sub-deployment>

Solution 2

One thing you didn't indicate was whether or not you added a log4j library in your EAR/lib folder as well. I assume you probably have though or you should be seeing other errors.

I'm not sure whether or not log4j would have a log4j.xml inside a JAR inside your EAR to be honest. I would think that the EAR/META-INF would be a more appropriate place for your log4j configuration file.

There is no real reason to remove the logging subsystem in this case. Also, I'm not trying to convince you to use it, but you could create a custom handler fairly easily to do what you're looking to do. You could base it on the SizeRotatingFileHandler and then just add a suffix on the rename.

Share:
14,667
TYS
Author by

TYS

Updated on July 14, 2022

Comments

  • TYS
    TYS almost 2 years

    I have been reading a lot on this forum,jboss docs and on the internet to successfully get log4j configuration to work with jboss as 7.1.1, I do not want to use the logging subsystem in jboss. I want to use my own log4j configuration. My jboss server is configured in standalone mode. The following is what I did to get log4j configured based on docs :

    1. Define a jboss-deployment-structure.xml as per https://docs.jboss.org/author/display/AS71/How+To#HowTo-HowdoIuselog4j.propertiesorlog4j.xmlinsteadofusingtheloggingsubsystemconfiguration%3F, and I added this in the META-INF directory of my EAR
    2. I added a log4j.xml as is, and packaged inside a jar in the lib directory of my ear.
    3. Removed the logging subsystem and the extension module="org.jboss.as.logging" from standalone.xml.
    4. I did not change the logging.properties that is provided as a startup parameter in the startup.sh as I've read that is the logging that the jboss server will use before the subsystem kicks in.

    Inspite of doing all of this I cannot get the application to log as per my log4j configuration.

    My reason to use my own log4j configuration instead of the logging subsytem is to be able to use a custom rolling file appender for size-rotating-file-handler, as I want the rotated files to have a timestamp attached to the file name.

    Help appreciated,

    Ok, so I created a class MyHandler.java that extends from the SizeRotatingFileHandler.java and I override the preWrite method. MyHandler.java is in a package a.b.c. I create a sub directory under modules a/b/c and inside the c directory I add a jar that has just the Myhandler.class file. I add a module.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <module xmlns="urn:jboss:module:1.0" name="a.b.c">
         <resources>
               <resource-root path="RotatingHandler.jar"/>
              <!-- Insert resources here -->
         </resources>
         <dependencies>
              <module name="org.jboss.logging"/>
         </dependencies>
    </module>
    

    Then in the standalone.xml I added an entry for the custom handler

    <custom-handler name="FILE3" class="a.b.c.MyHandler" module="a.b.c">
            <level name="INFO"/>
            <formatter>
                <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
            </formatter>
            <file relative-to="jboss.server.log.dir" path="srveree.log"/>
        </custom-handler>
    

    When I start Jboss it says it cant find the class a.b.c.MyHandler. How do I resolve this error ?

    UPDATE : I resolved this error. There was a problem in the package structure inside the module. However, I am still going back to the original question of configuring log4j with jboss-as-7.1.1.Final.