From 7290dba23a3c19c3d61f82aaccb37bbea592a5b2 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Sat, 6 Mar 2010 18:17:28 +0000 Subject: [PATCH] Added logging framework --- src/zutil/MultiPrintStream.java | 29 ----- src/zutil/log/LogFormatter.java | 15 +++ src/zutil/log/Logger.java | 136 ++++++++++++++++++++++++ src/zutil/log/StandardLogFormatter.java | 98 +++++++++++++++++ 4 files changed, 249 insertions(+), 29 deletions(-) create mode 100644 src/zutil/log/LogFormatter.java create mode 100644 src/zutil/log/Logger.java create mode 100644 src/zutil/log/StandardLogFormatter.java diff --git a/src/zutil/MultiPrintStream.java b/src/zutil/MultiPrintStream.java index 91cc65e..c5d0c4d 100644 --- a/src/zutil/MultiPrintStream.java +++ b/src/zutil/MultiPrintStream.java @@ -8,7 +8,6 @@ import java.io.PrintStream; import java.io.Reader; import java.lang.reflect.Array; import java.lang.reflect.Field; -import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; @@ -21,10 +20,6 @@ import java.util.Map; public class MultiPrintStream extends PrintStream { //the print streams that will print private ArrayList streams; - // the stream should print time stamp - private boolean timeStamp = false; - //The timestamp style - private String timeStampString = "yyyy-MM-dd HH:mm:ss:SSS# "; //a instance of this class public static MultiPrintStream out = new MultiPrintStream(); @@ -114,7 +109,6 @@ public class MultiPrintStream extends PrintStream { * Prints with a new line to all the PrintStreams */ public void println(String s){ - if(!s.equals(""))s = getTime() + s; for(int i=0; i class_log_level; + + // 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(); + } + + /** + * @return the parent class other than Logger in the stack + */ + protected static String getCalingClass(){ + StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); + for(int i=1; i 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); + } +} diff --git a/src/zutil/log/StandardLogFormatter.java b/src/zutil/log/StandardLogFormatter.java new file mode 100644 index 0000000..4a75087 --- /dev/null +++ b/src/zutil/log/StandardLogFormatter.java @@ -0,0 +1,98 @@ +package zutil.log; + +import java.text.SimpleDateFormat; +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 = false; + //The time stamp style + private String timeStampString = "yyyy-MM-dd HH:mm:ss:SSS#"; + // 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; + + + public String format(String source, Level level, String msg) { + StringBuilder data = new StringBuilder(); + if( timeStamp && className ){ + data.append( getTime() ); + data.append( " " ); + data.append( paddSourceName(source) ); + data.append( ": " ); + } + else if( timeStamp ){ + data.append( getTime() ); + data.append( " " ); + } + else if( className ){ + data.append( paddSourceName(source) ); + data.append( ": " ); + } + + StringBuilder ret = new StringBuilder(); + String[] array = splitter.split( msg ); + for( int i=0; i 0 ) + ret.append( data ); + ret.append( array[i] ); + } + 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){ + timeStampString = ts; + } + + /** + * @return the time stamp + */ + private String getTime(){ + return (new SimpleDateFormat(timeStampString)).format(new java.util.Date()).toString(); + } + + /** + * 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){ + if( source.length() > max_class_name ) + max_class_name = source.length(); + + StringBuilder sb = new StringBuilder( source ); + for( int i=source.length(); i