Added logging framework
This commit is contained in:
parent
7bad0e2c18
commit
7290dba23a
4 changed files with 249 additions and 29 deletions
|
|
@ -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<PrintStream> 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<streams.size() ;i++)
|
||||
streams.get(i).println(s);
|
||||
}
|
||||
|
|
@ -147,29 +141,6 @@ public class MultiPrintStream extends PrintStream {
|
|||
public void print(Object x){ print(String.valueOf(x));}
|
||||
|
||||
|
||||
/**
|
||||
* If the streams should print time stamp in front
|
||||
* of the msgs
|
||||
* @param enable True to activate
|
||||
*/
|
||||
public void printTimeStamp(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;
|
||||
}
|
||||
|
||||
private String getTime(){
|
||||
if(timeStamp)
|
||||
return "" + (new SimpleDateFormat(timeStampString)).format(new java.util.Date());
|
||||
else
|
||||
return "";
|
||||
}
|
||||
|
||||
public boolean checkError(){
|
||||
for(int i=0; i<streams.size() ;i++)
|
||||
|
|
|
|||
15
src/zutil/log/LogFormatter.java
Normal file
15
src/zutil/log/LogFormatter.java
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
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);
|
||||
}
|
||||
136
src/zutil/log/Logger.java
Normal file
136
src/zutil/log/Logger.java
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
package zutil.log;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import zutil.MultiPrintStream;
|
||||
|
||||
/**
|
||||
* This is a logger class
|
||||
*
|
||||
* @author Ziver
|
||||
*
|
||||
*/
|
||||
public class Logger {
|
||||
// This is the global log level
|
||||
protected static Level global_log_level;
|
||||
// The OutputStream
|
||||
protected static PrintStream out;
|
||||
// The formatter class that formats the output
|
||||
protected static LogFormatter formatter;
|
||||
// Class Specific log level
|
||||
protected static HashMap<String, Level> 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<String, Level>();
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 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);
|
||||
}
|
||||
}
|
||||
98
src/zutil/log/StandardLogFormatter.java
Normal file
98
src/zutil/log/StandardLogFormatter.java
Normal file
|
|
@ -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<array.length ;++i ){
|
||||
if( i!=0 )
|
||||
ret.append( '\n' );
|
||||
if( data.length() > 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<max_class_name; ++i ) {
|
||||
sb.append( ' ' );
|
||||
}
|
||||
return sb.toString();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue