1 Reply Latest reply on May 3, 2004 8:50 AM by Chuck Harris

    Logging problems with log4j

    Chuck Harris Newbie

      I have migrated an application from an jboss2.4.1_tomcat3.2.3 to jb3.2.2. Logging is via
      log4j and I am trying to integrate with the version that is used by jb3.2.2. I have a
      Logger.java (at end) class that allowed logging to occur from within jboss and when the
      class is run from the commandline to test. For jboss3.2.2, I needed to comment several
      lines to even allow jboss to start. Each class calls an initializer (at end) and then the
      Logger method addLoggingForClass(String pClassName) is called.
      Commented lines:
      PropertyConfigurator.configure(m_configProps) and
      Category acat = getCategory(pClassName);
      m_categories.put(pClassName,acat);
      acat.log(pClassName, Priority.INFO, "Setting up Logging for " + pClassName, null);
      since it caused error :
      ERROR: invalid console appender config detected, console stream is looping

      I believe the original developer used examples from the original version of log4j.
      By commenting these lines, my logging is displayed in the console and a rolling file.
      If I run a class from the commandline rather than from within jboss, there is no logging at all. I tried to use
      DOMConfigurator dc =new DOMConfigurator();
      dc.configure("/packages/jboss-3.2.2/server/default/conf/log4j.xml");
      in the Logger constructor but that caused the same error as above.
      I would like logging to work however the class is run, either from within jboss or from the commandline. How do I get logging to work from the command line but still allow the logging to continue working when from within jboss?

      import java.util.Properties;
      import org.apache.log4j.Category;
      import com.codestudio.sql.PoolMan;
      
      import util.ResourceUtils;
      import util.BaseException;
      import util.Logger;
      import org.apache.log4j.xml.DOMConfigurator;
      
      /**
       * This class performs System Initialization, e.g., setting up
       * Logging, auditing, pooling, configurations, etc.
       * The class cannot be instantiated.
       * @see #initialize() for invoking the initializer properly.
       */
      public abstract class Initializer
      {
      
       static Category cat = Category.getInstance("base.pack");
       static boolean isInitialized = false;
       /**
       * Initialize the System
       */
       public static void initialize()
       {
       if (isInitialized) return;
       try
       {
      
       System.out.println("Initializing Logging");
       initLogging();
      
       isInitialized=true;
       }
       catch(BaseException e)
       {
       Logger.fatal("base.pack","Fatal error initializing",e);
       }
       }
      
       private static void initPools() throws BaseException
       {
       try
       {
       PoolMan.start();
       }
       catch (Exception e)
       {
       throw new BaseException("Error Initializing Pools.", e);
       }
       }
       private static void initLogging() throws BaseException
       {
       try
       {
       //System.out.println("********* start init logging");
       //Properties p = ResourceUtils.getProperties(Initializer.class, "/log.config");
       //Properties p = new Properties();
       //System.out.println("********* init loggging '" + p.toString() + "'");
      
       // if (p==null)
       // {
       //System.out.println("p is null");
       // throw new BaseException("Cannot load Properties");
       // }
      
       //System.out.println("********* props not null " + p.toString());
      
       Logger log = new Logger("base.pack");
       Logger.debug("base.pack", "******* Logging started");
       }
       catch (Exception e)
       {
       System.out.println("fatal error and we are hosed " + e);
       throw new BaseException("Fatal Error Initializing", e);
       }
       }
      }



      package util;
      
      import org.apache.log4j.PropertyConfigurator;
      import org.apache.log4j.NDC;
      import org.apache.log4j.Category;
      import org.apache.log4j.Priority;
      import java.util.Properties;
      import java.util.HashMap;
      
      import org.apache.log4j.xml.DOMConfigurator;
      /**
       * This class kicks off the logging mechanism for the system
       * and needs to be called at program startup
       *
       */
      
      public class Logger
      {
      
       //private String m_category = null;
       private Properties m_configProps= null;
      
       private static HashMap m_categories = new HashMap(5);
       DOMConfigurator dc;
       //static org.apache.log4j.Logger log;
      
      /** The constructor kicks off the logging mechanism.
       * @param pCategory is the category that logging starts with
       * which is typically the main class.
       * @param pConfigFile is the name of the config file that is
       * used to initialize the logging mechanism and set it's properties
       */
       public Logger(String pCategory, Properties pConfigFile)
       {
       //System.out.println("*********logger has been initiated 0 Logger.java");
       // m_configProps = pConfigFile;
      
       //System.out.println("*********logger has been initiated 1 Logger.java");
       // Category cat = Category.getInstance(pCategory);
      
       // System.out.println("*********logger has been initiated 2 Logger.java");
       // m_categories.put(pCategory,cat);
       // System.out.println("*********logger has been initiated 3 Logger.java");
       // PropertyConfigurator.configure(m_configProps); // todo java2
       //System.out.println("*********logger has been initiated 4 Logger.java");
       }
      
       public Logger(String pClassName)
       {
       //System.out.println("*********logger has been initiated 0 Logger.java");
       // m_configProps = pConfigFile;
      
       //System.out.println("*********logger has been initiated 1 Logger.java");
       // Category cat = Category.getInstance(pCategory);
      
       // System.out.println("*********logger has been initiated 2 Logger.java");
       // m_categories.put(pCategory,cat);
       // System.out.println("*********logger has been initiated 3 Logger.java");
       // PropertyConfigurator.configure(m_configProps);
       //System.out.println("*********logger has been initiated 4 Logger.java");
      // dc = new DOMConfigurator();
      // dc.configure("/packages/jboss-3.2.2/server/default/conf/log4j.xml");
       }
      
       /**
       * This method should be run in a static block in any class
       * that wishes to do logging. This will guarantee that logging
       * can be done for that class 'category' (won't be null).
       * Logging must be initialized within the jvm with the constructor
       * before this method can be invoked.
       * @see #Logger(String pCategory, Properties pConfigFile)
       */
       public static void addLoggingForClass(String pClassName)
       {
       System.out.println("setting up logging for " + pClassName);
       //Category acat = getCategory(pClassName);
       //m_categories.put(pClassName,acat);
       //acat.log(pClassName, Priority.INFO, "Setting up Logging for " + pClassName, null);
       //log = org.apache.log4j.Logger.getLogger(pClassName);
       }
      
      
       private static Category getCategory(String p)
       {
       if (m_categories.containsKey(p))
       {
       return(Category) m_categories.get(p);
       }
       else
       {
       Category newcat =
       Category.getInstance(p);
       m_categories.put(p,newcat);
       return newcat;
       }
      
       }
      
       public static void debug(String pClassName, Object pMsg)
       {
       Category acat = getCategory(pClassName);
       acat.log(pClassName, Priority.DEBUG, pMsg, null);
       }
      
       public static void info(String pClassName, Object pMsg)
       {
       Category acat = getCategory(pClassName);
       acat.log(pClassName, Priority.INFO, pMsg,null);
       }
      
       public static void warn(String pClassName, Object pMsg)
       {
       Category acat = getCategory(pClassName);
       acat.log(pClassName, Priority.WARN, pMsg, null);
       }
      
       public static void warn(String pClassName, Object pMsg, Throwable pT)
       {
       Category acat = getCategory(pClassName);
       acat.log(pClassName, Priority.WARN, pMsg, pT);
       }
      
       public static void error(String pClassName, Object pMsg, Throwable pT)
       {
       Category acat = getCategory(pClassName);
       acat.log(pClassName, Priority.ERROR, pMsg, pT);
       }
      
       public static void error(String pClassName, Object pMsg)
       {
       Category acat = getCategory(pClassName);
       acat.log(pClassName, Priority.ERROR, pMsg,null);
       }
      
       /**
       * TODO!!! get requirements for what to do on a fatal error
       * send emails, alert the media, make pagers go off, that sort of thing..
       */
       public static void fatal(String pClassName, Object pMsg, Throwable pT)
       {
       Category acat = getCategory(pClassName);
       acat.log(pClassName, Priority.FATAL, pMsg + " \n\n\n\n\n\nFATAL ERROR\n\n\n\n\n\n\n\n", pT);
       //System.exit(999);
       }
      
       public static void push(Object p)
       {
       NDC.push(p.toString());
       }
      
       public static void pop()
       {
       NDC.pop();
       }
      
      }