User: user57  
  Date: 02/01/03 16:14:17

  Modified:    src/main/org/jboss/system BootstrapLogger.java
  Log:
   o fixed problem with BootstrapLogger which prevented it from ever
     initializing and proxying to Log4j
   o Added a org.jboss.system.BootstrapLogger.threshold prop which will be
     used until log4j is up to limit whichs priorities are enabled.
   o If log4j is not up yet, then check if the pri is enabled before printing
     anything.
  
  Revision  Changes    Path
  1.3       +168 -96   jboss/src/main/org/jboss/system/BootstrapLogger.java
  
  Index: BootstrapLogger.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/system/BootstrapLogger.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- BootstrapLogger.java      2002/01/03 04:01:00     1.2
  +++ BootstrapLogger.java      2002/01/04 00:14:17     1.3
  @@ -15,42 +15,57 @@
    * from the system classpath. It uses reflection to obtain access to the log4j
    * classes using the thread context class loader.
    *
  - * @author  [EMAIL PROTECTED]
  - * @version $Revision: 1.2 $
  + * @author  <a href="mailto:[EMAIL PROTECTED]";>Scott Stark</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
  + * @version $Revision: 1.3 $
    */
   public class BootstrapLogger
   {
      /** The log4j Category class name */
      private static final String CATEGORY_CLASS = "org.apache.log4j.Category";
  +
      /** The log4j Priority class name */
      private static final String PRIORITY_CLASS = "org.apache.log4j.Priority";
  +   
      /** The custom JBoss TRACE Priority class name */
      private static final String TRACE_PRIORITY_CLASS =  
"org.jboss.logging.TracePriority";
  +   
      /** Indicies into the log4jMethods for the Category methods */
      private static final int GET_INSTANCE = 0;
      private static final int IS_ENABLED_FOR_PRIORITY = 1;
      private static final int LOG_PRIORITY_MSG = 2;
      private static final int LOG_PRIORITY_MSG_EX = 3;
      private static final int N_METHODS = 4;
  +   
  +   /** An array of the Log4j priority names. */
  +   private static final String[] PRIORITY_NAMES = {
  +      "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
  +   };
  +
      /** An array of the org.apache.log4j.Category methods used by BootstrapLogger */
      private static Method[] log4jMethods = null;
  -   /** An array of the org.apache.log4j.Category methods used by BootstrapLogger */
  -   private static String[] priorityNames = {"TRACE", "DEBUG", "INFO", "WARN",
  -      "ERROR", "FATAL"
  -   };
  +      
      /** An array of Priority objects corresponding to the names TRACE..FATAL */
  -   private static Object[] log4jPriorities = new Object[priorityNames.length];
  +   private static Object[] log4jPriorities = new Object[PRIORITY_NAMES.length];
  +   
      private static final int TRACE = 0;
      private static final int DEBUG = 1;
      private static final int INFO = 2;
      private static final int WARN = 3;
      private static final int ERROR = 4;
      private static final int FATAL = 5;
  +   
      /** Should execptions during the load of log4j be dumped to System.err */
      private static boolean logInitFailures = false;
  +   
      /** The maximum # of initLog4j calls to attempt */
      private static int maxInitAttempts = 100;
  +   
  +   /** The number of initialization attempts so far. */
      private static int initAttempts;
  +   
  +   /** The threashold used when uninitialized */
  +   private static int threshold = INFO;
   
      // Externalize behavior using properties
      static
  @@ -58,8 +73,8 @@
         try
         {
            logInitFailures = 
Boolean.getBoolean("org.jboss.system.BootstrapLogger.logInitFailures");
  -         Integer i = 
Integer.getInteger("org.jboss.system.BootstrapLogger.maxInitAttempts", 
maxInitAttempts);
  -         maxInitAttempts = i.intValue();
  +         maxInitAttempts = 
Integer.getInteger("org.jboss.system.BootstrapLogger.maxInitAttempts", 
maxInitAttempts).intValue();
  +         threshold = 
Integer.getInteger("org.jboss.system.BootstrapLogger.threshold", threshold).intValue();
         }
         catch(Exception e)
         {
  @@ -69,255 +84,312 @@
   
      /** The log4j Category object the BootstrapLogger delegates to. */
      private Object category;
  +   
  +   /** The category type. */
      private String categoryType;
   
  -   public static BootstrapLogger getLogger(Class categoryType)
  +   public static BootstrapLogger getLogger(Class type)
      {
  -      return getLogger(categoryType.getName());
  +      return getLogger(type.getName());
      }
  -   public static BootstrapLogger getLogger(String categoryType)
  +   
  +   public static BootstrapLogger getLogger(String typename)
      {
         try
         {
            initLog4j();
  -      }
  -      catch(Exception ignore)
  -      {
         }
  -      return new BootstrapLogger(categoryType);
  +      catch(Exception ignore) {}
  +
  +      return new BootstrapLogger(typename);
      }
   
  -// --- Begin log4j Category methods we expose
  +   // --- Begin log4j Category methods we expose
  +   
      public void trace(Object msg)
      {
         log(TRACE, msg);
      }
  +   
      public void trace(Object msg, Throwable ex)
      {
         log(TRACE, msg, ex);
      }
  +   
      public void debug(Object msg)
      {
         log(DEBUG, msg);
      }
  +   
      public void debug(Object msg, Throwable ex)
      {
         log(DEBUG, msg, ex);
      }
  +   
      public void info(Object msg)
      {
         log(INFO, msg);
      }
  +   
      public void info(Object msg, Throwable ex)
      {
         log(INFO, msg, ex);
      }
  +   
      public void warn(Object msg)
      {
         log(WARN, msg);
      }
  +   
      public void warn(Object msg, Throwable ex)
      {
         log(WARN, msg, ex);
      }
  +   
      public void error(Object msg)
      {
         log(ERROR, msg);
      }
  +   
      public void error(Object msg, Throwable ex)
      {
         log(ERROR, msg, ex);
      }
  +   
      public void fatal(Object msg)
      {
         log(FATAL, msg);
      }
  +   
      public void fatal(Object msg, Throwable ex)
      {
         log(FATAL, msg, ex);
      }
  +   
      public boolean isTraceEnabled()
      {
         return isEnabledFor(TRACE);
      }
  +   
      public boolean isDebugEnabled()
      {
         return isEnabledFor(DEBUG);
      }
  +   
      public boolean isInfoEnabled()
      {
         return isEnabledFor(INFO);
      }
   
  -// --- Begin log4j Category methods we expose
  -   /** This method is called by all isXXXEnabled methods to invoke
  -    the Category.isEnabledForPriority method using the priority instance.
  -    If the log4j methods have not been loaded then the priority
  +   // --- Begin log4j Category methods we expose
  +   
  +   /** 
  +    * This method is called by all isXXXEnabled methods to invoke
  +    * the Category.isEnabledForPriority method using the priority instance.
  +    * If the log4j methods have not been loaded then the priority
       */
      private boolean isEnabledFor(int priorityIdx)
      {
         boolean isEnabled = false;
  +      
         try
         {
            if( category == null )
            {
               // Try to load the log4j classes
               init();
  -            // Return false if we still don't have a category
  -            if( category == null )
  -               return false;
  +         
  +            // if we don't have a category the use our threshold
  +            if (category == null) {
  +               return priorityIdx >= threshold;
  +            }
            }
  +         
            Object priority = log4jPriorities[priorityIdx];
            Object[] args = {priority};
  -         Boolean bool = (Boolean) 
log4jMethods[IS_ENABLED_FOR_PRIORITY].invoke(category, args);
  +         Boolean bool = (Boolean)
  +            log4jMethods[IS_ENABLED_FOR_PRIORITY].invoke(category, args);
            isEnabled = bool.booleanValue();
         }
         catch(Exception e)
         {
            e.printStackTrace();
         }
  +   
         return isEnabled;
      }
   
  -   /** The log method is called by all explicit priority log methods
  -    that do not accept a Throwable.
  -    @param priorityIdx, the index into the log4jPriorities array to use as
  -      the priority of the msg
  -    @param msg, the log message object
  +   /** 
  +    * The log method is called by all explicit priority log methods
  +    * that do not accept a Throwable.
  +    *
  +    * @param idx   the index into the log4jPriorities array to use as
  +    *              the priority of the msg
  +    * @param msg   the log message object
       */
  -   private void log(int priorityIdx, Object msg)
  +   private void log(int idx, Object msg)
      {
  -      // If there is no category log4j is not yet available
  -      if( category == null )
  -      {
  -         // Try to load the log4j classes
  -         init();
  -         if( category == null )
  -         {
  -            // Failed, dump the msg to System.out
  -            String name = priorityNames[priorityIdx];
  -            System.out.println(name+", "+msg);
  -            return;
  -         }
  -      }
  -
  -      // Invoke the Category.log method
  -      try
  -      {
  -         Object priority = log4jPriorities[priorityIdx];         
  -         Object[] args = {priority, msg};
  -         log4jMethods[LOG_PRIORITY_MSG].invoke(category, args);
  -      }
  -      catch(Exception e)
  -      {
  -         e.printStackTrace();
  -      }
  +      _log(idx, msg, Void.class);
      }
   
  -   /** The log method is called by all explicit priority log methods
  -    that do accept a Throwable.
  -    @param priorityIdx, the index into the log4jPriorities array to use as
  -      the priority of the msg
  -    @param msg, the log message object
  -    @param ex, the exception associated with the msg
  +   /** 
  +    * The log method is called by all explicit priority log methods
  +    * that do accept a Throwable.
  +    *
  +    * @param idx   the index into the log4jPriorities array to use as
  +    *              the priority of the msg
  +    * @param msg   the log message object
  +    * @param ex    the exception associated with the msg
       */
  -   private void log(int priorityIdx, Object msg, Throwable ex)
  +   private void log(int idx, Object msg, Throwable ex)
      {
  +      _log(idx, msg, ex);
  +   }
  +
  +   /**
  +    * Consolidates both logging calls.  If extends is Void.class then
  +    * the one argument log() method will be invoked, else the two
  +    * arg version will be used.
  +    */
  +   private void _log(int idx, Object msg, Object ex) {
  +      // if we don't have a category yet, they try to initialize
         if( category == null )
         {
            // Try to load the log4j classes
            init();
            if( category == null )
            {
  +            if (!isEnabledFor(idx)) return;
  +
               // Failed, dump the msg to System.out & print stack trace
  -            String name = priorityNames[priorityIdx];
  -            System.out.println(name+", "+msg);
  -            ex.printStackTrace();
  +            System.out.println(PRIORITY_NAMES[idx] + " " + msg);
  +            
  +            // If extends is not Void.class then it is an exception
  +            if (ex != Void.class) {
  +               if (ex != null) {
  +                  ((Throwable)ex).printStackTrace();
  +               }
  +               else {
  +                  System.err.println(ex);
  +               }
  +            }
  +            
               return;
            }
         }
   
  +      // Category is loaded, try logging to it
         try
         {
  -         Object priority = log4jPriorities[priorityIdx];         
  -         Object[] args = {priority, msg, ex};
  -         log4jMethods[LOG_PRIORITY_MSG_EX].invoke(category, args);
  +         Object pri = log4jPriorities[idx];         
  +         Object[] args;
  +         if (ex != Void.class) {
  +            args = new Object[] { pri, msg, ex };
  +            log4jMethods[LOG_PRIORITY_MSG_EX].invoke(category, args);
  +         }
  +         else {
  +            args = new Object[] { pri, msg};
  +            log4jMethods[LOG_PRIORITY_MSG].invoke(category, args);
  +         }
         }
  -      catch(Exception e)
  +      catch (Exception e)
         {
            e.printStackTrace();
         }
      }
  -// End log4j methods
  +
  +   // End log4j methods
   
  -   private BootstrapLogger(String categoryType)
  +   private BootstrapLogger(final String categoryType)
      {
         this.categoryType = categoryType;
         init();
      }
   
  -   /** This method is called to set the logger category and retry loading
  -    the log4j classes if they have not been loaded as yet.
  +   /** 
  +    * This method is called to set the logger category and retry loading
  +    * the log4j classes if they have not been loaded as yet.
       */
      private void init()
      {
  -      try
  -      {
  -         if( log4jMethods == null )
  +      try {
  +         if (log4jMethods == null) {
               initLog4j();
  -         if( log4jMethods != null )
  -         {
  +         }
  +         
  +         if (log4jMethods != null) {
               Object[] args = {categoryType};
               category = log4jMethods[GET_INSTANCE].invoke(null, args);
            }
         }
  -      catch(Exception e)
  -      {
  -         if( logInitFailures == true )
  +      catch (ClassNotFoundException e) {
  +         if (logInitFailures == true) {
               e.printStackTrace();
  +         }
         }
  +      catch (Exception e)
  +      {
  +         // If we get something other than an CNFE then something
  +         // is probably wrong
  +         e.printStackTrace();
  +      }
      }
   
  -   /** Load the log4j classes using the thread context class loader and
  -    build an array of the methods and priorities we use in this class
  -    using reflection.
  +   /** 
  +    * Load the log4j classes using the thread context class loader and
  +    * build an array of the methods and priorities we use in this class
  +    * using reflection.
       */
  -   private static synchronized void initLog4j() throws ClassNotFoundException,
  -      NoSuchMethodException, IllegalAccessException, IllegalArgumentException,
  -      InvocationTargetException
  +   private static synchronized void initLog4j() 
  +      throws ClassNotFoundException,
  +             NoSuchMethodException, 
  +             IllegalAccessException, 
  +             IllegalArgumentException,
  +             InvocationTargetException
      {
  -      if( log4jMethods != null || initAttempts > 100 )
  +      if (log4jMethods != null || initAttempts > maxInitAttempts) {
            return;
  +      }
   
         initAttempts ++;
  +      
         ClassLoader loader = Thread.currentThread().getContextClassLoader();
  +      Class type;
  +      
         // Load the custom trace Priority class and log4j Priority
  -      Class trace = loader.loadClass(TRACE_PRIORITY_CLASS);
         Class priorityClass = loader.loadClass(PRIORITY_CLASS);
   
         // Fill in the standard log4j Priority instances
         Class[] toPriorityTypes = {String.class};
         Method toPriority = priorityClass.getDeclaredMethod("toPriority", 
toPriorityTypes);
  -      for(int n = 1; n < priorityNames.length; n ++)
  +      for (int n = 1; n < PRIORITY_NAMES.length; n ++)
         {
  -         Object[] args = {priorityNames[n]};
  +         Object[] args = { PRIORITY_NAMES[n] };
            log4jPriorities[n] = toPriority.invoke(null, args);
         }
  -      toPriority = trace.getDeclaredMethod("toPriority", toPriorityTypes);
  +      
  +      // Add the custom TRACE priority
  +      Class traceClass = loader.loadClass(TRACE_PRIORITY_CLASS);
         toPriorityTypes = new Class[] {String.class, priorityClass};
  +      toPriority = traceClass.getDeclaredMethod("toPriority", toPriorityTypes);
         log4jPriorities[0] = toPriority.invoke(null, new Object[]{"TRACE", 
log4jPriorities[1]});
   
         // Build an array of the log4j Category methods we use
  -      Method[] tmp = new Method[N_METHODS];
         Class categoryClass = loader.loadClass(CATEGORY_CLASS);
  +      Method[] tmp = new Method[N_METHODS];
  +
         Class[] paramTypes = {String.class};
         tmp[GET_INSTANCE] = categoryClass.getDeclaredMethod("getInstance", 
paramTypes);
  +      
         paramTypes = new Class[] {priorityClass};
         tmp[IS_ENABLED_FOR_PRIORITY] = 
categoryClass.getDeclaredMethod("isEnabledFor", paramTypes);
  +      
         paramTypes = new Class[] {priorityClass, Object.class};
  -      tmp[LOG_PRIORITY_MSG] = categoryClass.getDeclaredMethod("log", paramTypes);
  +      tmp[LOG_PRIORITY_MSG] = categoryClass.getMethod("log", paramTypes);
  +      
         paramTypes = new Class[] {priorityClass, Object.class, Throwable.class};
  -      tmp[LOG_PRIORITY_MSG_EX] = categoryClass.getDeclaredMethod("log", paramTypes);
  +      tmp[LOG_PRIORITY_MSG_EX] = categoryClass.getMethod("log", paramTypes);
  +      
         // The log4jMethods is only assigned if all methods were found
         log4jMethods = tmp;
      }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to