hal/src/zutil/log/CompactLogFormatter.java

169 lines
5.3 KiB
Java
Raw Normal View History

2015-05-27 13:13:19 +00:00
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ziver Koc
2013-05-28 19:29:24 +00:00
*
2011-07-13 17:53:17 +00:00
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2013-05-28 19:29:24 +00:00
*
2011-07-13 17:53:17 +00:00
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
2013-05-28 19:29:24 +00:00
*
2011-07-13 17:53:17 +00:00
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
2015-05-27 13:13:19 +00:00
*/
2013-05-28 19:29:24 +00:00
2011-07-13 17:53:17 +00:00
package zutil.log;
2015-10-15 19:26:17 +00:00
import zutil.StringUtil;
import zutil.io.StringOutputStream;
2011-07-13 17:53:17 +00:00
import java.io.PrintStream;
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;
public class CompactLogFormatter extends Formatter{
2015-10-15 19:26:17 +00:00
/** The split pattern where the **/
2011-07-13 17:53:17 +00:00
private static final Pattern splitter = Pattern.compile("\n");
2015-10-15 19:26:17 +00:00
/** the stream should print time stamp **/
2011-07-13 17:53:17 +00:00
private boolean timeStamp = true;
2015-10-15 19:26:17 +00:00
/** The time stamp style **/
2015-04-14 22:16:36 +00:00
private SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
2015-10-15 19:26:17 +00:00
/** If displaying class names are enabled **/
2011-07-13 17:53:17 +00:00
private boolean className = true;
2015-10-15 19:26:17 +00:00
/** If displaying method names are enabled **/
2011-07-13 17:53:17 +00:00
private boolean methodName = false;
2015-10-15 19:26:17 +00:00
/** Specifies the max length of the longest class name **/
2011-07-13 17:53:17 +00:00
private int max_class_name = 0;
2015-10-15 19:26:17 +00:00
/** Cache for the class padding **/
2011-07-13 17:53:17 +00:00
private static HashMap<String,String> padd_cache = new HashMap<String,String>();
2015-10-15 19:26:17 +00:00
/** Date temp file **/
2011-07-13 17:53:17 +00:00
private Date date = new Date();
2015-10-15 19:26:17 +00:00
2011-07-13 17:53:17 +00:00
@Override
public String format(LogRecord record) {
2015-10-15 19:26:17 +00:00
StringBuilder prefix = new StringBuilder();
2011-07-13 17:53:17 +00:00
if( timeStamp ){
date.setTime( record.getMillis() );
2015-10-15 19:26:17 +00:00
prefix.append(dateFormatter.format(date));
prefix.append(' ');
2011-07-13 17:53:17 +00:00
}
switch( record.getLevel().intValue() ){
2015-10-15 19:26:17 +00:00
case /* SEVERE */ 1000: prefix.append("[SEVERE] "); break;
case /* WARNING */ 900 : prefix.append("[WARNING]"); break;
case /* INFO */ 800 : prefix.append("[INFO] "); break;
case /* CONFIG */ 700 : prefix.append("[CONFIG] "); break;
case /* FINE */ 500 : prefix.append("[FINE] "); break;
case /* FINER */ 400 : prefix.append("[FINER] "); break;
case /* FINEST */ 300 : prefix.append("[FINEST] "); break;
2011-07-13 17:53:17 +00:00
}
2015-10-15 19:26:17 +00:00
prefix.append(' ');
2011-07-13 17:53:17 +00:00
if( className ){
2015-10-15 19:26:17 +00:00
prefix.append(paddClassName(record.getSourceClassName()));
2011-07-13 17:53:17 +00:00
}
if(methodName){
2015-10-15 19:26:17 +00:00
prefix.append(record.getSourceMethodName());
2011-07-13 17:53:17 +00:00
}
2015-10-15 19:26:17 +00:00
prefix.append(": ");
2011-07-13 17:53:17 +00:00
2015-10-15 19:26:17 +00:00
StringBuilder ret = new StringBuilder();
2011-07-13 17:53:17 +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' );
2015-10-15 19:26:17 +00:00
if( prefix.length() > 0 )
ret.append( prefix );
2011-07-13 17:53:17 +00:00
ret.append( array[i] );
}
2015-04-09 21:14:25 +00:00
ret.append( '\n' );
2011-07-13 17:53:17 +00:00
}
2015-04-09 21:14:25 +00:00
2011-07-13 17:53:17 +00:00
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' );
2015-10-15 19:26:17 +00:00
if( prefix.length() > 0 )
ret.append( prefix );
2011-07-13 17:53:17 +00:00
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 enableClassName(boolean enable){
className = enable;
}
/**
* 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;
}
/**
* @return the Class name
*/
private String paddClassName(String source){
String tmp = padd_cache.get(source);
2015-10-15 19:26:17 +00:00
if(tmp != null && tmp.length() == max_class_name)
2011-07-13 17:53:17 +00:00
return tmp;
String c_name = source.substring( source.lastIndexOf('.')+1 );
if( c_name.length() > max_class_name )
max_class_name = c_name.length();
2015-10-15 19:26:17 +00:00
c_name += StringUtil.getSpaces(max_class_name - c_name.length());
padd_cache.put(source, c_name);
return c_name;
2011-07-13 17:53:17 +00:00
}
}