nyla.solutions.global.util.Debugger Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nyla.solutions.global Show documentation
Show all versions of nyla.solutions.global Show documentation
Nyla Solutions Global Java API provides support for basic application
utilities (application configuration, data encryption, debugger and text
processing).
The newest version!
package nyla.solutions.global.util;
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
import nyla.solutions.global.exception.ConfigException;
import nyla.solutions.global.exception.SetupException;
import nyla.solutions.global.operations.logging.Log;
import nyla.solutions.global.util.Config;
import nyla.solutions.global.util.Debugger;
import nyla.solutions.global.util.JavaBean;
/**
*
*
* Debugger provides useful methods for obtaining exception stack traces.
* It can build reader friendly strings for objects
* that do not implement their toString method.
*
* It also provides a set of print functions
* to log DEBUG, INFO, WARN and FATAL level messages using
* the Debugger.println(...), Debugger.printInfo(...),
* Debugger.printWarn(...) and Debugger.printFatal(...) methods respectively.
*
* The default log object implementation is solutions.global.operations.Log4J.
*
* Set the configuration property to plug-in another logger (@see Config more information);
*
* Log.class=className
*
* The logClass class name indicated must implement the
* nyla.solutions.global.operations.Log interface.
*
* Sample Property File
*
*#-------------------------------
*# Debugger log instance uses Log4J
*# You can directly include the log4J properties in the configuration property files (@see Config object)
*# Log4J properties
*#
*log4j.rootLogger=DEBUG, stdout
*log4j.logger.PACKAGE_NAME=ERROR,file_error
*log4j.logger.YYY=DEBUG, file_all
*log4j.logger.org.apache=ERROR,stdout
*log4j.logger.org.springframework=ERROR,stdout
*
*#Standard OUT
*log4j.appender.stdout=org.apache.log4j.ConsoleAppender
*log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
*#log4j.appender.stdout.layout.ConversionPattern=%d [%F:%L] - %x %m%n
*#log4j.appender.stdout.layout.ConversionPattern= %p: %d{HH:mm:ss} [%F:%L] - %x %m%n
*#log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss} [%c:%L] %m%n
*#log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss} %m%n
*log4j.appender.stdout.layout.ConversionPattern=%p: %d [%c] - %m%n
*
*#
*# FILE Output
*log4j.file_all.category=DEBUG
*log4j.appender.file_all=org.apache.log4j.RollingFileAppender
*log4j.appender.file_all.File=/temp/logs/system.log
*log4j.appender.file_all.MaxFileSize=10MB
*log4j.appender.file_all.MaxBackupIndex=3
*log4j.appender.file_all.layout=org.apache.log4j.PatternLayout
*log4j.appender.file_all.layout.ConversionPattern=%p: %d [%c] - %m%n
*
*
*#
*# FILE Output
*#log4j.file_error.category=ERROR
*log4j.appender.file_error=org.apache.log4j.RollingFileAppender
*log4j.appender.file_error.File=temp/logs/error.log
*log4j.appender.file_error.MaxFileSize=10MB
*log4j.appender.file_error.MaxBackupIndex=3
*log4j.appender.file_error.layout=org.apache.log4j.PatternLayout
*log4j.appender.file_error.layout.ConversionPattern=%p: %d [%c] - %m%n
*
*
*#Emailing example
*#email appender
*log4j.appender.mail=org.apache.log4j.net.SMTPAppender
*log4j.appender.mail.BufferSize=1
*log4j.appender.mail.SMTPHost=smtp.myservername.xx
*[email protected]
*[email protected]
*log4j.appender.mail.Subject=Log ...
*log4j.appender.mail.threshold=error
*log4j.appender.mail.layout=org.apache.log4j.PatternLayout
*log4j.appender.mail.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
*
*
*
* @author Gregory Green
* @version 1.5
*/
public class Debugger
{
/**
* LOG_CLASS_NAME_PROP = "Log.logClass"
*/
public static final String LOG_CLASS_NAME_PROP = "Log.class";
private static Class> logClass;
private static HashMap, Log> logMap = new HashMap, Log>();
static
{
try
{
logClass = Class.forName(Config.getProperty(LOG_CLASS_NAME_PROP,
"nyla.solutions.global.operations.logging.Log4J"));
defaultLogger = (Log) logClass.newInstance();
//String configFile = Config.getLocation();
//if(configFile != null)
// getLog(Debugger.class).debug(
// "CONFIG: properties loaded from " +configFile );
//else
//getLog(Debugger.class).debug(
// "CONFIG: NO properties loaded");
}
catch(SetupException e)
{
throw e;
}
catch(ConfigException e)
{
throw e;
}
catch (RuntimeException e)
{
throw new SetupException(e);
}
catch (NoClassDefFoundError e)
{
throw new SetupException("Check value of "+LOG_CLASS_NAME_PROP+" in confi file"+Config.getLocation(),e);
}
catch (ClassNotFoundException e)
{
throw new SetupException("Check value of "+LOG_CLASS_NAME_PROP+" in confi file"+Config.getLocation(),e);
}
catch (IllegalAccessException e)
{
throw new SetupException("Check value of "+LOG_CLASS_NAME_PROP+" in confi file"+Config.getLocation(),e);
}
catch (InstantiationException e)
{
throw new SetupException("Check value of "+LOG_CLASS_NAME_PROP+" in confi file"+Config.getLocation(),e);
}
} // ------------------------------------------------------
/**
*
*
*
* @param c the calling class
*
* @return the log 4 J category
*/
/**
*
* @param c the calling class
* @return the logger implementation
*/
@SuppressWarnings("rawtypes")
public static Log getLog(Class c)
{
try
{
if (c == null || logClass == null)
return defaultLogger;
// check cache
Log logger = logMap.get(c);
if (logger == null)
{
// create an put in map
logger = (Log) logClass.newInstance();
logger.setLoggingClass(c);
logMap.put(c, logger);
}
return logger;
}
catch (Exception e)
{
e.printStackTrace();
return defaultLogger;
}
} // -----------------------------------------------
/**
*
* convert throwable to a stack trace
*
* @param t the throwable
*
* @return the stack trace string
*/
public static String stackTrace(Throwable t)
{
if (t == null)
{
return "Debugger.stackTrace(null) NULL EXCEPTION (NO TRACE AVAILABLE)!!";
}
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
return sw.toString();
} // -----------------------------------------
/**
*
* @param obj input object
* @return the string representation
*/
public static String toString(Object obj)
{
return toString(obj, new HashSet