6 Replies Latest reply on Feb 9, 2012 5:21 AM by Prasad Deshpande

    log4j problems...

    markus78 Newbie

      Hello,

       

      Im having problems with log4j and latest JBoss AS 7 nightly,

       

      I have a very simple servlet that basically just initializes log4j and then writes a log message ( this is a small scale test  ) , with log4j on the classpath

       

      the servlet

       

      [code]

      package my.test; 

       

      import java.io.*; 

      import javax.servlet.ServletException; 

      import javax.servlet.annotation.WebServlet;

      import javax.servlet.http.HttpServlet;

      import javax.servlet.http.HttpServletRequest; 

      import javax.servlet.http.HttpServletResponse; 

      import org.apache.log4j.Logger;

      import org.apache.log4j.Priority;

      import org.apache.log4j.xml.DOMConfigurator;

       

      @WebServlet("/hello")

      public class testServlet extends HttpServlet { 

                private static final long serialVersionUID = 1L;

       

        private static Logger eventLog = null;

       

                @Override

                protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        

              DOMConfigurator.configure("c:\\projects\\log4j.properties.xml");

              eventLog = Logger.getLogger("EVENT_LOG");      

              eventLog.log(Priority.INFO,"TEST TEST");

       

          }    

       

          protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {           

          }                

      [/code]

       

      And c:\projects\log4j.properties.xml is also very simple

       

      <log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'>

       

         <appender name="EVENT" class="org.apache.log4j.FileAppender">     

            <param name="File"   value="c:\\temp\\event.log" />

            <param name="Append" value="true" />

                     <layout class="org.apache.log4j.PatternLayout">

                                              <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />

                            </layout>

         </appender>

       

         <logger name="EVENT_LOG">

            <level value="INFO" />

            <appender-ref ref="EVENT" />

         </logger>

       

         <root>

         </root>

      </log4j:configuration>

       

       

      But triggering the servlet only causes my "test-test" message to be dumped onto the console and not into the c:\temp\event.log as I expect.

       

      14:54:29,431 INFO  [EVENT_LOG] (http-executor-threads - 8) TEST TEST

       

      And when I try to debug this I endup in the jboss logmanager "BridgeLogger" class

       

       

         public void addAppender(final Appender newAppender) {

              // ignored

          }

       

      So it seems none of my appenders are added....

       

      The mini-example above works fine in CR1b but not with the latest nightly build.

       

      Any help would be great!

      /Markus

        • 1. Re: log4j problems...
          markus78 Newbie

          Tried this workaround , https://community.jboss.org/thread/194849 , and it solved my problems to.

           

          I have added 2 WAR's for easy testing, the one named working does work (woa..) it has the jboss-deployment-structure.xml under WEB-INF in the war , the one marked not working does not have this xml file.

           

          /Markus

          • 2. Re: log4j problems...
            Prasad Deshpande Expert

            Markus, I couldn't get logs written to file even after using exclusions as you said in the other post.. I'll have better look tomorrow..

            • 3. Re: log4j problems...
              markus78 Newbie

              Thanks! , I have attached 2 new WAR's and verified that the behaviour is as I claimed earlier ( I could not get the EAR from the other thread to log to a file either ), so you can use these WAR's for testing instead.

               

              /Markus

              • 4. Re: log4j problems...
                Prasad Deshpande Expert

                I haven't tried these two yet, but looking at packaging of ear in other post, the only reason for ear not woring despite using exclusion could be due to placement of jboss-deployment-structure.xml in war within ear (& log4j.jar in ear/lib) & classloader heirarchy. Since ear/meta-inf doesn't contain jboss-deployment-structure.xml, when a demand for log4j class will come, it will be first deligated to parent classloader of ear & hence  . If jboss-deployment-structure.xml is added to ear/meta-inf, then ear will filter log4j classes & instead will return from ear/lib.. so it then might be able to see appender defined in log4j.properties.xml.

                 

                WAR deployed on it's own & ear are similar.. so I think just having jboss-deployment-structure.xml in war (deployed on it's own) will work (as if ear having jboss-deployment-structure.xml in ear/meta-inf)..

                 

                But, I'll try myself this tomorrow just to verify my thinking...

                • 5. Re: log4j problems...
                  markus78 Newbie

                  Hi again,

                   

                  I did some more testing and was able to get the EAR version working to, but I had to use this jboss-deployment-structure.xml in the EAR's META-INF folder

                   

                  <?xml version="1.0" encoding="UTF-8"?>

                  <jboss-deployment-structure>

                     <deployment>

                       <exclusions>

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

                       </exclusions>

                     </deployment>

                     <sub-deployment name="servletLog.war">

                       <exclusions>

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

                       </exclusions>

                     </sub-deployment>

                  </jboss-deployment-structure>

                   

                  Shouldn't the "top" deployment exclude org.apache.log4j from all subdeployments?

                   

                  BR

                  /Markus

                  • 6. Re: log4j problems...
                    Prasad Deshpande Expert

                    Same happend with me, I couldn't get EAR working yesterday.. although, I didn't try adding sub-deployment part...

                     

                    But I agree with you.., if you put that file in ear/meta-inf, you shouldn't need to add sub-deployment tag.. that information should have been propagated to all WAR's within..  I think this could be a bug.. if not, I'd need to understand classloading in this case as logically this behaviour doesn't sound correct to me either..