User: starksm
Date: 02/02/12 02:40:19
Modified: src/main/org/jboss/system BootstrapLogger.java
Log:
General cleanup and comments
Revision Changes Path
1.5 +101 -111 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.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- BootstrapLogger.java 4 Jan 2002 00:33:39 -0000 1.4
+++ BootstrapLogger.java 12 Feb 2002 10:40:19 -0000 1.5
@@ -10,20 +10,20 @@
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
-/**
+/**
* A utility class that allows the use of log4j by classes that must be loaded
* from the system classpath. It uses reflection to obtain access to the log4j
* classes using the thread context class loader.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Scott Stark</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason Dillon</a>
- * @version $Revision: 1.4 $
+ * @version $Revision: 1.5 $
*/
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";
@@ -41,13 +41,13 @@
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 Priority objects corresponding to the names TRACE..FATAL */
private static Object[] log4jPriorities = new Object[PRIORITY_NAMES.length];
-
+ /** The indcies into the log4jPriorities array */
private static final int TRACE = 0;
private static final int DEBUG = 1;
private static final int INFO = 2;
@@ -66,10 +66,15 @@
/** The threashold used when uninitialized */
private static int threshold = INFO;
-
+
/** True to enable more verbose logging when log4j is not yet initialized. */
private static boolean verbose = false;
-
+
+ /** The placeholder passed to the log(int,Object,Throwable) method by
+ the log methods that do not take a Throwable.
+ */
+ private static final Throwable NO_EXCEPTION = new Throwable();
+
// Externalize behavior using properties
static
{
@@ -85,13 +90,13 @@
e.printStackTrace();
}
}
-
+
/** The log4j Category object the BootstrapLogger delegates to. */
private Object category;
/** The category type. */
private String categoryType;
-
+
public static BootstrapLogger getLogger(Class type)
{
return getLogger(type.getName());
@@ -103,91 +108,93 @@
{
initLog4j();
}
- catch(Exception ignore) {}
-
+ catch(Exception ignore)
+ {
+ }
+
return new BootstrapLogger(typename);
}
-
+
// --- Begin log4j Category methods we expose
public void trace(Object msg)
{
- log(TRACE, msg);
+ log(TRACE, msg, NO_EXCEPTION);
}
-
+
public void trace(Object msg, Throwable ex)
{
log(TRACE, msg, ex);
}
-
+
public void debug(Object msg)
{
- log(DEBUG, msg);
+ log(DEBUG, msg, NO_EXCEPTION);
}
-
+
public void debug(Object msg, Throwable ex)
{
log(DEBUG, msg, ex);
}
-
+
public void info(Object msg)
{
- log(INFO, msg);
+ log(INFO, msg, NO_EXCEPTION);
}
-
+
public void info(Object msg, Throwable ex)
{
log(INFO, msg, ex);
}
-
+
public void warn(Object msg)
{
- log(WARN, msg);
+ log(WARN, msg, NO_EXCEPTION);
}
-
+
public void warn(Object msg, Throwable ex)
{
log(WARN, msg, ex);
}
-
+
public void error(Object msg)
{
- log(ERROR, msg);
+ log(ERROR, msg, NO_EXCEPTION);
}
-
+
public void error(Object msg, Throwable ex)
{
log(ERROR, msg, ex);
}
-
+
public void fatal(Object msg)
{
- log(FATAL, msg);
+ log(FATAL, msg, NO_EXCEPTION);
}
-
+
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
+ // --- End 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
@@ -195,76 +202,44 @@
private boolean isEnabledFor(int priorityIdx)
{
boolean isEnabled = false;
-
+
try
{
if( category == null )
{
// Try to load the log4j classes
init();
-
+
// if we don't have a category the use our threshold
- if (category == null) {
+ 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;
- }
- /**
- * A magical token to tell _log() which log4j varaity to use.
- *
- * If this value is passed as the second argument to _log(), then
- * the single argument log4j method will be called.
- */
- private static final Object MAGIC_TOKEN = new 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 idx, Object msg)
- {
- _log(idx, msg, MAGIC_TOKEN);
+ return isEnabled;
}
- /**
- * The log method is called by all explicit priority log methods
- * that do accept a Throwable.
- *
+ /** Called by the xxx(Object) and xxx(Object,Throwable) methods.
* @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
+ * @param ex the exception associated with the msg or the special token
+ * NO_EXCEPTION to indicate no exception was passed in.
*/
private void log(int idx, Object msg, Throwable ex)
{
- _log(idx, msg, ex);
- }
-
- /**
- * Consolidates both logging calls. If ex is MAGIC_TOKEN 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 we don't have a category yet, then try to initialize
if( category == null )
{
// Try to load the log4j classes
@@ -272,11 +247,12 @@
if( category == null )
{
if (!isEnabledFor(idx)) return;
-
+
// construct the message to print
StringBuffer buff = new StringBuffer();
buff.append(PRIORITY_NAMES[idx]).append(" ");
- if (verbose) {
+ if (verbose)
+ {
buff.append("[").append(categoryType).append("] ");
}
buff.append(msg);
@@ -284,16 +260,19 @@
// Failed, dump the msg to System.out & print stack trace
System.out.println(buff);
- // If ex is not MAGIC_TOKEN then it is an exception
- if (ex != MAGIC_TOKEN) {
- if (ex != null) {
- ((Throwable)ex).printStackTrace();
+ // If ex is not NO_EXCEPTION then it is an exception
+ if (ex != NO_EXCEPTION)
+ {
+ if (ex != null)
+ {
+ ex.printStackTrace();
}
- else {
+ else
+ {
System.err.println(ex);
}
}
-
+
return;
}
}
@@ -301,14 +280,16 @@
// Category is loaded, try logging to it
try
{
- Object pri = log4jPriorities[idx];
+ Object priority = log4jPriorities[idx];
Object[] args;
- if (ex != MAGIC_TOKEN) {
- args = new Object[] { pri, msg, ex };
+ if (ex != NO_EXCEPTION)
+ {
+ args = new Object[] { priority, msg, ex };
log4jMethods[LOG_PRIORITY_MSG_EX].invoke(category, args);
}
- else {
- args = new Object[] { pri, msg};
+ else
+ {
+ args = new Object[] { priority, msg};
log4jMethods[LOG_PRIORITY_MSG].invoke(category, args);
}
}
@@ -319,31 +300,36 @@
}
// End log4j methods
-
+
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.
*/
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 (ClassNotFoundException e) {
- if (logInitFailures == true) {
+ catch (ClassNotFoundException e)
+ {
+ if (logInitFailures == true)
+ {
e.printStackTrace();
}
}
@@ -355,30 +341,33 @@
}
}
- /**
+ /**
* 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()
+ private static synchronized void initLog4j()
throws ClassNotFoundException,
- NoSuchMethodException,
- IllegalAccessException,
- IllegalArgumentException,
- InvocationTargetException
+ NoSuchMethodException,
+ IllegalAccessException,
+ IllegalArgumentException,
+ InvocationTargetException
{
- if (log4jMethods != null || initAttempts > maxInitAttempts) {
+ if (log4jMethods != null || initAttempts > maxInitAttempts)
+ {
return;
}
-
initAttempts ++;
-
+
+ /* Use the current thread context class loader as it will eventual
+ * have access to the log4j classes provided the Log4jService is
+ *deployed.
+ */
ClassLoader loader = Thread.currentThread().getContextClassLoader();
- Class type;
// Load the custom trace Priority class and log4j Priority
Class priorityClass = loader.loadClass(PRIORITY_CLASS);
-
+
// Fill in the standard log4j Priority instances
Class[] toPriorityTypes = {String.class};
Method toPriority = priorityClass.getDeclaredMethod("toPriority",
toPriorityTypes);
@@ -392,12 +381,13 @@
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]});
+ Object[] args = {"TRACE", log4jPriorities[1]};
+ log4jPriorities[0] = toPriority.invoke(null, args);
// Build an array of the log4j Category methods we use
Class categoryClass = loader.loadClass(CATEGORY_CLASS);
Method[] tmp = new Method[N_METHODS];
-
+
Class[] paramTypes = {String.class};
tmp[GET_INSTANCE] = categoryClass.getDeclaredMethod("getInstance",
paramTypes);
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development