2 Replies Latest reply on Feb 8, 2011 10:45 AM by Glenn Puckett

    JBoss, log4j and EBJ 2

    Glenn Puckett Newbie

      I am really struggling with getting my logging working.  I have a mature web application that has been in production for about 4 years.  It runs in JBoss 4.3.3.GA using Java v1.6.  The application is developed using EJB 2 and Struts.  My development environment is MyEclipse IDE with the enterprise capability turned on.  I am using log4j for all my logs.  I am using v1.2.13 of log4j. I have the application deployed in the default server of  JBoss.  So I put the ear file in d:\jboss\server\default\deploy and the log4j.jar file in d:\jboss\server\deploy\lib.

       

      At first I could get all my log messages out but it was always mixed in with the normal jboss messages.  Eventually I developed my own process of logging using log4j.   I'll provide details below.  Essentially I am now at a point where I have discovered that any logging that comes out of the EJB project doesn't go to a log file.  I have entered debug mode and followed the code.  It shows that debug mode is one and the message is generated but I can't find it in any of the application OR JBoss log files.  In my action classes in the WEB project I include code from the EJB project.  I get the debug messages from the WEB code but not the EJB code.

       

      It's driving me crazy!!

       

      Here is my log4j properties file:

       

      log4j.rootLogger=ERROR, console
      log4j.category.MyApp=DEBUG, applog 
       
      **********************************************
      *   applog is a DailyRollingFileAppender     * 
      **********************************************
      log4j.appender.applog=org.jboss.logging.appender.DailyRollingFileAppender
      log4j.appender.applog.file=D:/MyApp/logs/web.log
      log4j.appender.applog.datePattern='.'yyyy-MM-dd'.log'
      log4j.appender.applog.append=true
      log4j.appender.applog.threshold=DEBUG
      log4j.appender.applog.immediateFlush=true
      log4j.appender.applog.layout=org.apache.log4j.PatternLayout
      log4j.appender.applog.layout.ConversionPattern=%-5p %d{ISO8601} [%t] %C{1}::%M:%L %m%n
      
      **********************************************
      *   console for everything else              *
      **********************************************
      
      log4j.appender.console=org.apache.log4j.ConsoleAppender
      log4j.appender.console.Threshold=ERROR
      log4j.appender.console.layout=org.apache.log4j.PatternLayout
      log4j.appender.console.layout.ConversionPattern=%d %5p %c (%F:%L) – %m%n
      
      *********************************************
      *   Now set up the thresholds by class      *
      *********************************************
      
      log4j.logger.MyApp.com=DEBUG
      log4j.logger.MyApp.mm=DEBUG 
      

       

      I have custom code that accesses the configuration.  I call it WebLogger

       

      package com.myapp.common.logger;
      
      import org.apache.log4j.Logger;
      import org.apache.log4j.PropertyConfigurator;
      
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.InputStream;
      import java.util.Properties;
      
      import com.myapp.common.exception.*;
      
      public class WebLogger 
      {
           private WebLogger() {
           }
           public static Logger getLogger(Class tCls) 
                               throws ErrorException
           {
                return getLogger(tCls.getName());
           }
           
           public static Logger getLogger(String tLvl) 
                                    throws ErrorException
           {
                Logger rtn = Logger.getLogger(tLvl);
                if(rtn.getParent().getName().equals("root")) {
                     initLogger();
                     rtn = Logger.getLogger(tLvl);
                }
                return rtn;
           }
           
           private static void initLogger() 
                               throws ErrorException
           {
                System.setProperty("log4j.defaultInitOverride", "true");
                ClassLoader cl = WebLogger.class.getClassLoader();
                String appHome = System.getenv("MYAPP_HOME");
                try {
                     File fIn = new File(appHome+"\\lib", "log4j.properties");
                     if(fIn.exists()) {
                          InputStream is = new FileInputStream(fIn);
                          cl.loadClass("org.apache.log4j.PropertyConfigurator");
                          Properties p = new Properties();
                          p.load(is);
                          PropertyConfigurator.configure(p);
                     }
                } catch (Exception e) {
                     throw new ErrorException("Unable to initialize the logger. Error msg is: "+e.getMessage());
                }
           }
      
      }
      
      

       

      In my code I initialize the logger as:   (APP_PKG contains "MyApp.")

       

      Logger logger = com.myapp.common.logger.WebLogger.getLogger(APP_PKG + MyClass.class.getName());

       


      All of the ejb packages start with mm.myapp.  None of the web packages start with that.

      Again, the log messages from the web container is going Where is the output from my code going?

       

        • 1. JBoss, log4j and EBJ 2
          Wolfgang Knauf Master

          Hi,

           

          JBoss (at least the 5.x branch, but I think that 4.x also did this) already bundles Log4J, so you don't need to include it in your own applications.

          The JBoss bundled Log4j is configured by "server\default\conf\jboss-log4j.xml", so you should add your configuration here.

           

          See here: http://community.jboss.org/wiki/logging

           

          Hope this helps

           

          Wolfgang

          • 2. JBoss, log4j and EBJ 2
            Glenn Puckett Newbie

            Thanks for the response.

             

            In my original configuration I put the application logging setup in jboss-log4j.xml.  But no matter what I did I couldn't get the JBoss logs separated from my application logs.  It always ended up with the two getting mixed into BOTH sets of logs.  And I tired several different ways of setting it up.  That's why I ended up going with this solution.  At first it seemed to be working fine.  But evidently at some point I installed a minor release of JBoss that has new ways of handling Log4j.

             

            I can go back and give it a new try and see what happens.  But that is not what it appears most folks are doing.  I have read a lot about imbedding your logging configuration in the EAR file.  But I can't get the log4j.jar file shared in both the EJB and WEB containers.