Remove proprietary Logging class and added Java Logging util class

This commit is contained in:
Ziver Koc 2010-05-18 20:38:02 +00:00
parent 06a9d49a40
commit afac27d828
4 changed files with 66 additions and 288 deletions

View file

@ -1,15 +0,0 @@
package zutil.log;
import java.util.logging.Level;
public interface LogFormatter {
/**
* This method formats a log message in a specific way
*
* @param source is the class that sent the log
* @param level is the severity of the log message
* @param msg is the log message
*/
public String format( String source, Level level, String msg);
}

View file

@ -0,0 +1,66 @@
package zutil.log;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Utility functions for the standard Java Logger
*
* @author Ziver
*/
public class LogUtil {
private LogUtil(){}
/**
* @return a new Logger for the calling class
*/
public static Logger getLogger(){
return Logger.getLogger(getCalingClass());
}
/**
* @return the parent class other than Logger in the stack
*/
public static String getCalingClass(){
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
for(int i=1; i<stackTraceElements.length ;++i){
String name = stackTraceElements[i].getClassName();
//name = name.substring( name.lastIndexOf('.')+1 );
if( !name.equals( LogUtil.class.getName() ) )
return name;
}
return null;
}
/**
* Sets the global log formatter to the specified one
*
* @param f is the formatter class
*/
public static void setGlobalFormatter(Formatter f){
Logger root = Logger.getLogger("");
for (Handler handler : root.getHandlers()) {
handler.setFormatter(f);
}
}
/**
* Sets the global log level
*/
public static void setGlobalLogLevel(Level level){
setLevel("", level);
}
/**
* Sets the log level for a specified logger
*/
public static void setLevel(String name, Level level){
Logger root = Logger.getLogger("");
root.setLevel(level);
for (Handler handler : root.getHandlers()) {
handler.setLevel(level);
}
}
}

View file

@ -1,163 +0,0 @@
package zutil.log;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.logging.Level;
import zutil.MultiPrintStream;
import zutil.wrapper.StringOutputStream;
/**
* This is a logger class
*
* @author Ziver
*
*/
public class Logger {
// This is the global log level
protected static Level global_log_level = Level.WARNING;
// Class Specific log level
protected static HashMap<String, Level> class_log_level;
// The OutputStream
protected static PrintStream out;
// The formatter class that formats the output
protected static LogFormatter formatter;
// The class that is logging
private String source;
/**
* Creates an Logger instance that is tied to the calling class
*/
public Logger(){
this( getCalingClass() );
}
/**
* Creates an Logger instance that is tied to the a specific class
*
* @param logging_class is the class that will be displayed as the logger
*/
public Logger(Class<?> logging_class){
this( logging_class.getSimpleName() );
}
/**
* Creates an Logger instance that is tied to the a specific source
*
* @param source is the string that will be displayed as the logger
*/
public Logger(String source){
this.source = source;
if(global_log_level == null)
global_log_level = Level.ALL;
if(out == null)
out = MultiPrintStream.out;
if(formatter == null)
formatter = new StandardLogFormatter();
if(class_log_level == null)
class_log_level = new HashMap<String, Level>();
}
/**
* Sets the global log level
*/
public static void setGlobalLogLevel(Level l){
global_log_level = l;
}
/**
* Sets an specific log level for an class
*/
public static void setClassLogLevel(Class<?> c, Level l){
class_log_level.put(c.getSimpleName(), l);
}
/**
* @return the parent class other than Logger in the stack
*/
protected static String getCalingClass(){
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
for(int i=1; i<stackTraceElements.length ;++i){
String name = stackTraceElements[i].getClassName();
name = name.substring( name.lastIndexOf('.')+1 );
if( !name.equals( Logger.class.getSimpleName() ) )
return name;
}
return null;
}
/**
* @param formater is the LogFormater that will be used
*/
public void setFormater(LogFormatter formater){
Logger.formatter = formater;
}
/**
* @param out is the PrintStream that the logs will be sent to
*/
public void setPrintStream(PrintStream out){
Logger.out = out;
}
/**
* Logs the given exception
*
* @param level is the severity of the exception
* @param e is the exception
*/
public void printStackTrace(Level level, Exception e){
StringOutputStream msg = new StringOutputStream();
e.printStackTrace( new PrintStream(msg) );
log(level, msg.toString());
}
/**
* Logs an message based on level
*
* @param level is the level of the message
* @param msg is the message to log
*/
public void log(Level level, String msg){
log(level, source, msg);
}
/**
* Logs an message based on level
*
* @param level is the level of the message
* @param source is the source class
* @param msg is the message to log
*/
public static void log(Level level, Class<?> source, String msg){
log(level, source.getSimpleName(), msg);
}
/**
* Logs an message based on level
*
* @param level is the level of the message
* @param source is the source
* @param msg is the message to log
*/
public static void log(Level level, String source, String msg){
// Check if message should be logged
if( class_log_level.containsKey(source) ){
if( class_log_level.get(source).intValue() > level.intValue() )
return;
}
else if( global_log_level.intValue() > level.intValue() )
return;
// This message should be logged
if( formatter != null )
msg = formatter.format(source, level, msg);
if(out != null)
out.println(msg);
else
MultiPrintStream.out.println(msg);
}
}

View file

@ -1,110 +0,0 @@
package zutil.log;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.regex.Pattern;
public class StandardLogFormatter implements LogFormatter{
// The split pattern where the
private static final Pattern splitter = Pattern.compile("\n");
// the stream should print time stamp
private boolean timeStamp = true;
//The time stamp style
private SimpleDateFormat dateFormatter = new SimpleDateFormat("HH:mm:ss ");
// If displaying class names are enabled
private boolean className = true;
// Specifies the max length of the longest class name
private int max_class_name = 0;
// Cache for the class padding
private static HashMap<String,String> padd_cache = new HashMap<String,String>();
// Date temp file
private Date date = new Date();
public String format(String source, Level level, String msg) {
StringBuilder data = new StringBuilder();
if( timeStamp ){
date.setTime( System.currentTimeMillis() );
data.append( dateFormatter.format(date) );
}
switch( level.intValue() ){
case /* SEVERE */ 1000: data.append("SEVERE "); break;
case /* WARNING */ 900 : data.append("WARNING "); break;
case /* INFO */ 800 : data.append("INFO "); break;
case /* CONFIG */ 700 : data.append("CONFIG "); break;
case /* FINE */ 500 : data.append("FINE "); break;
case /* FINER */ 400 : data.append("FINER "); break;
case /* FINEST */ 300 : data.append("FINEST "); break;
}
if( className ){
data.append( paddSourceName(source) );
}
data.append( ": " );
StringBuffer ret = new StringBuffer();
String[] array = splitter.split( msg );
for( int i=0; i<array.length ;++i ){
if( i!=0 )
ret.append( '\n' );
if( data.length() > 0 )
ret.append( data );
ret.append( array[i] );
}
ret.append( '\n' );
return ret.toString();
}
/**
* If the formatter should add a time stamp in front of the log message
*
* @param enable set to True to activate time stamp
*/
public void enableTimeStamp(boolean enable){
timeStamp = enable;
}
/**
* The DateFormat to print in the time stamp
*
* @param ts is the String to send to SimpleDateFormat
*/
public void setTimeStamp(String ts){
dateFormatter = new SimpleDateFormat(ts);
}
/**
* If the formatter should add the class/source name in front of the log message
*
* @param enable set to True to activate class/source name
*/
public void enableSourceName(boolean enable){
className = enable;
}
/**
* @return the Class name
*/
private String paddSourceName(String source){
String tmp = padd_cache.get(source);
if( tmp != null )
return tmp;
if( source.length() > max_class_name )
max_class_name = source.length();
StringBuilder sb = new StringBuilder( source );
for( int i=source.length(); i<max_class_name; ++i ) {
sb.append( ' ' );
}
tmp = sb.toString();
padd_cache.put(source, tmp);
return tmp;
}
}