Using application's Log4J configuration under JBoss 7.1.1

15,786

Solution 1

Hi RedEagle see the following configuration which i have tested and its working fine...
Step-1 Create a new module as

jboss-as-7.1.1.Final/modules/com/company/mylog/main/

                          -module.xml
                          -log4j-1.2.14.jar

Content of  module.xml


 <?xml version="1.0" encoding="UTF-8"?>
      <module xmlns="urn:jboss:module:1.0" name="com.company.mylog">
    <resources>
      <resource-root path="log4j-1.2.14.jar"/>
    </resources>
    <dependencies>
      <module name="javax.api"/>
         </dependencies>
      </module>

Step-2 Now IN my.ear/META-INF/

          -jboss-deployment-structure.xml
          -MANIFEST.MF

content of jboss-deployment-structure.xml

 <jboss-deployment-structure>
              <deployment>

                  <exclusions>
                  <module name="org.apache.log4j" />
                  </exclusions>

              </deployment>


              <sub-deployment name="MyWeb.war">
                    <exclusions>
                    <module name="org.apache.log4j" />
                    </exclusions>
              </sub-deployment>

              <sub-deployment name="MyBeans.jar">
                <exclusions>
                  <module name="org.apache.log4j" />
                </exclusions>
              </sub-deployment>


              </jboss-deployment-structure>

Content of MANIFEST.MF

        Manifest-Version: 1.0
        Dependencies: com.company.mylog

Step-3 Content of MyLogger.java

public static Logger getLogger(String name) {

    Logger  logger= LogManager.getLogger(name);
    PropertyConfigurator.configure("log4j.properties"); //Path to log4j.properties as many option available in my case for testing i used static path /home/gyani/log4j.properties
    return logger;
}

Step-4 Here is log4j.properties

log4j.rootLogger=info,gyani
log4j.appender.gyani=org.apache.log4j.RollingFileAppender
log4j.appender.gyani.File=/home/gyani/myserverlog.log
log4j.appender.gyani.Append=true
log4j.appender.gyani.MaxFileSize=100000KB
log4j.appender.gyani.MaxBackupIndex=10
log4j.appender.gyani.layout=org.apache.log4j.PatternLayout
log4j.appender.gyani.layout.ConversionPattern=[%d{MMM d HH:mm:ss yyyy}] [%-5p] [%c]: %m%n

Solution 2

Your jboss-deployment-structure.xml needs to be in your MyAppEAR.ear/META-INF directory. There is also no need for the slot attribute for this case.

Also the -Dorg.jboss.as.logging.per-deployment=false isn't used in JBoss AS 7.1.1.Final, but it doesn't hurt anything either.

Share:
15,786
RedEagle
Author by

RedEagle

Updated on August 02, 2022

Comments

  • RedEagle
    RedEagle over 1 year

    I'm am having trouble logging using my appenders defined on my XML log4j configuration file.

    I created the jboss-deployment-structure.xml on my EAR's META-INF folder with no sucess.

    The jboss-deployment-structure.xml structure is:

    <jboss-deployment-structure>
    <ear-subdeployments-isolated>false</ear-subdeployments-isolated>
        <deployment>
            <exclusions>
                <module name="org.apache.log4j" slot="main"/>
            </exclusions>
        </deployment>
    </jboss-deployment-structure>
    

    I have even tried to edit my standalone.conf.bat file adding the following line:

    set "JAVA_OPTS=%JAVA_OPTS% -Dorg.jboss.as.logging.per-deployment=false"
    

    My application deployment is like this:

    -> MyAppEAR.ear
       -> META-INF
          -> MANIFEST.MF
       -> MyAoo.war
          -> META-INF
             -> MANIFEST.MF
             -> jboss-deployment-structure.xml
          -> WEB-INF
             -> web.xml
             -> lib
                -> log4j-1.2.17.jar
                -> ---
             -> classes
                -> log4j.xml
                -> ...
    

    I've noticed the following error:

    • jboss-deployment-structure.xml in subdeployment ignored. jboss-deployment-structure.xml is only parsed for top level deployments.

    I even tried migrating from JBOSS 7.1.0 to 7.1.1

    Some help please!

    Thanks


    Thanks for the repply James.

    I did what you said and moved the jboss-deployment-structure.xml file to MyAppEAR.ear/META-INF.

    I've noticed that this way the exception:

    jboss-deployment-structure.xml in subdeployment ignored. jboss-deployment-structure.xml is only parsed for top level deployments.
    

    ... doesn't occur. I don't know if that means that the file was parsed... how can I tell?

    Despite of this new behaviour my log4j.xml configuration file still isn't loaded and the logger used still is Log4J's.

    I know this becaused I wrote to the console:

    System.out.println(Logger.getRootLogger().getClass().toString())
    

    ...and got:

    class org.jboss.logmanager.log4j.BridgeLogger
    

    I've also tried:

    • moving my log4j.xml to MyAppEAR.ear/META-INF.
    • removing the unecessary -Dorg.jboss.as.logging.per-deployment=false from standalone.conf.bat
    • removing slot from my jboss-deployment-structure.xml

    Any more ideas?

    Thanks

  • RedEagle
    RedEagle over 11 years
    Hello James. Any more ideas? Thanks
  • James R. Perkins
    James R. Perkins over 11 years
    Try moving the log4j library to MyAppEAR.ear/lib.
  • James R. Perkins
    James R. Perkins over 11 years
    Could you maybe post your log4j configuration file?
  • RedEagle
    RedEagle about 11 years
    Thanks gYanl! That worked! I didn't even need to to all those steps. All I did was create the jboss-deployment-structure.xml on the EAR's META-INF folder.