hal/src/zutil/log/CompactLogFormatter.java

147 lines
3.9 KiB
Java
Raw Normal View History

2010-04-15 20:52:34 +00:00
package zutil.log;
2011-06-24 23:20:59 +00:00
import java.io.PrintStream;
2010-04-15 20:52:34 +00:00
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
import java.util.regex.Pattern;
2011-06-24 23:20:59 +00:00
import zutil.io.StringOutputStream;
2010-04-15 20:52:34 +00:00
public class CompactLogFormatter extends Formatter{
// 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
2011-06-24 23:20:59 +00:00
private SimpleDateFormat dateFormatter = new SimpleDateFormat();
2010-04-15 20:52:34 +00:00
// If displaying class names are enabled
private boolean className = true;
// If displaying method names are enabled
private boolean methodName = false;
// 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();
2011-06-24 23:20:59 +00:00
2010-04-15 20:52:34 +00:00
@Override
public String format(LogRecord record) {
StringBuilder data = new StringBuilder();
2011-06-24 23:20:59 +00:00
2010-04-15 20:52:34 +00:00
if( timeStamp ){
date.setTime( record.getMillis() );
2011-06-24 23:20:59 +00:00
data.append( dateFormatter.format(date) );
data.append(' ');
2010-04-15 20:52:34 +00:00
}
2011-06-24 23:20:59 +00:00
2010-04-15 20:52:34 +00:00
switch( record.getLevel().intValue() ){
2011-06-24 23:20:59 +00:00
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;
2010-04-15 20:52:34 +00:00
}
2011-06-24 23:20:59 +00:00
data.append(' ');
2010-04-15 20:52:34 +00:00
if( className ){
data.append( paddClassName(record.getSourceClassName()) );
}
if(methodName){
data.append( record.getSourceMethodName() );
}
data.append( ": " );
2011-06-24 23:20:59 +00:00
2010-04-15 20:52:34 +00:00
StringBuffer ret = new StringBuffer();
2011-06-24 23:20:59 +00:00
if( record.getMessage() != null ){
String[] array = splitter.split( record.getMessage() );
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' );
if( record.getThrown() != null ){
StringOutputStream out = new StringOutputStream();
record.getThrown().printStackTrace(new PrintStream(out));
String[] array = splitter.split( out.toString() );
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' );
2010-04-15 20:52:34 +00:00
}
return ret.toString();
}
2011-06-24 23:20:59 +00:00
2010-04-15 20:52:34 +00:00
/**
* 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);
}
2011-06-24 23:20:59 +00:00
2010-04-15 20:52:34 +00:00
/**
* 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 enableClassName(boolean enable){
className = enable;
}
2011-06-24 23:20:59 +00:00
2010-04-15 20:52:34 +00:00
/**
* 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 enableMethodName(boolean enable){
methodName = enable;
}
2011-06-24 23:20:59 +00:00
2010-04-15 20:52:34 +00:00
/**
* @return the Class name
*/
private String paddClassName(String source){
String tmp = padd_cache.get(source);
if( tmp != null )
return tmp;
2011-06-24 23:20:59 +00:00
2010-04-15 20:52:34 +00:00
String c_name = source.substring( source.lastIndexOf('.')+1 );
if( c_name.length() > max_class_name )
max_class_name = c_name.length();
2011-06-24 23:20:59 +00:00
2010-04-15 20:52:34 +00:00
StringBuilder sb = new StringBuilder( c_name );
2011-06-24 23:20:59 +00:00
for( int i=c_name.length(); i<max_class_name; ++i ) {
sb.append( ' ' );
}
tmp = sb.toString();
padd_cache.put(source, tmp);
return tmp;
2010-04-15 20:52:34 +00:00
}
}