10 Replies Latest reply on Mar 14, 2006 12:58 PM by peterj

    commons-logging/log4j problem

    pi1

      Hi everybody, i'm using JBoss 4.0.2 and my problem is i want to use in my web app log4j over implementation of commons-logging Log4JLogger. So i created log4j.properties file in WEB-INF/classes:

      # Debug log4j
      log4j.debug=true

      log4j.rootLogger=debug, myapp

      log4j.appender.myapp=org.apache.log4j.FileAppender
      log4j.appender.myapp.layout=org.apache.log4j.HTMLLayout
      log4j.appender.myapp.layout.LocationInfo=true
      log4j.appender.myapp.layout.Title='All' Log
      log4j.appender.myapp.File=myapp.html
      log4j.appender.myapp.ImmediateFlush=true
      log4j.appender.myapp.Append=false

      In jboss-web.xml i added:
      <jboss-web>
      <class-loading java2ClassLoadingCompliance="false">
      <loader-repository>
      webtest:loader=webtest.war
      <loader-repository-config>
      java2ParentDelegation=false</loader-repository-config>
      </loader-repository>
      </class-loading>
      </jboss-web>

      I put log4j-1.2.9.jar to WEB-INF/lib.

      So what confuse me is when i use code like:

      import org.apache.log4j.Logger;
      ...
      Logger log=Logger.getLogger(TestServlet.class);
      log.debug("testing");

      everithing works fine, i get debug info from log4j how it configures logger and log file myapp.html contains my "testing" message.
      I need to use commons-logging so i use code like that:

      import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;
      ...
      Log log=LogFactory.getLog(TestServlet.class);
      log.debug("testing");

      in this case no debug info, no log file myapp.html.?
      when i try to force log4j reading log4j.properties like:

      import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;
      import org.apache.log4j.PropertyConfigurator;
      ...
      PropertyConfigurator.configure(Thread.currentThread().getContextClassLoader(). getResource("log4j.properties"));
      Log log=LogFactory.getLog(TestServlet.class);
      log.debug("testing");
      i got debug info from log4j, log file myapp.html was created, but was empty?

      Thanks for any future reply.

        • 1. Re: commons-logging/log4j problem
          pi1

          Some my observations. It seems to me as i saw when debugging webapp, that my log4j.properties are not picked up and logger
          Log log=LogFactory.getLog(TestServlet.class);
          i retrieve gets appenders from JBoss, so it puts all log messages to server.log and console, but not to my myapp.html log file.

          Can anybody help me to solve this problem?

          • 2. Re: commons-logging/log4j problem
            starksm64
            • 3. Re: commons-logging/log4j problem
              pi1

              I've read this wiki and i've done all accordingly to scoped logging to use my log4j.properties. Results i've got as in reply above. My question is how to log with commons-logging. When i log directly with log4j logger like:
              Logger log=Logger.getLogger(TestServlet.class);
              log.debug("testing");

              it works. I need to log with commons-logging like:

              Log log=LogFactory.getLog(TestServlet.class);
              log.debug("testing");

              In wiki i have not found anything about logging with commons-logging.
              Any reply can be helpfull for me, thanks.

              • 4. Re: commons-logging/log4j problem
                peterj

                Are you by any chance including the commons logging jar file in your WEB-INF/lib directory? If so, try removing it and see if that solves the problem. I used to use commons logging all the time in my applications and never had a problem getting my logging output from JBoss.

                • 5. Re: commons-logging/log4j problem
                  pi1

                  In my case removing commons logging jar file from WEB-INF/lib directory doesn't solve the problem. Do you specify your appenders in JBoss log4j xml configuration file?

                  • 6. Re: commons-logging/log4j problem
                    peterj

                    Yes I, specify my appenders in the JBoss log4j xml configuration file.

                    • 7. Re: commons-logging/log4j problem
                      pi1

                      Hi, PeterJ. If you define your appeners in JBoss log4j configuration file, how do you separate logs among your apps. Do you specify
                      <category name=com.myapp1>

                      <appender-ref ref="Appender1"/>

                      ...
                      <category name=com.myapp2>

                      <appender-ref ref="Appender2"/>

                      so classes in package com.myapp1.* log with Appender1 and classes in com.myapp2.* log with Appender2? I can not do so, as my diffrent apps classes in packeges with the same name com.myapp.*.
                      Or do you use TCLFilter to filter apps by deploymentURL while logging (i have not even got it work with this filter)? Also i really want to keep my log4j properties in my own file, so user can easily adjust this settings without touching JBoss configuration.

                      • 8. Re: commons-logging/log4j problem
                        pi1

                        I've found not so pretty solution how to force classloader to pick my own log4j.properties. I put implemenatation of Log4JLogger in my package e.c. com.myapp.Log4JLogger and added to commons-logging.properties line:

                        org.apache.commons.logging.Log=com.myapp.Log4JLogger

                        Maybe someone could help me to find more correct solution?

                        Regards.

                        • 9. Re: commons-logging/log4j problem
                          starksm64

                          That is all you can do if you are relying on configuration of singletons automatically finding a local configuration based on the class loader. A patch to the commons-logging to allow for more dynamic discovery of the logger factory is in the works. See http://jira.jboss.com/jira/browse/JBAS-2933

                          • 10. Re: commons-logging/log4j problem
                            peterj

                            For the work that I do, logging to the server.log is no problem for me, so I have not tried to separate the logging out to other files.

                            The main reason for my suggestion regarding the commons logging file location was the possibility that you are running into a class loader issue, which seems to be confirmed by your latest message. But after thinking about it some more, maybe I should have suggested that you put the commons logging jar file into your WEB-INF/lib directory.

                            By the way, one way of debugging a cloass loader issue is add the -verbose:class JVM option to the command line. The JVM will then tell you for each class loaded what jar file it came from.