Added Stream logger classes
This commit is contained in:
parent
4a817cbf31
commit
a04041c07c
5 changed files with 250 additions and 37 deletions
|
|
@ -24,6 +24,7 @@
|
||||||
|
|
||||||
package zutil.log;
|
package zutil.log;
|
||||||
|
|
||||||
|
import zutil.StringUtil;
|
||||||
import zutil.io.StringOutputStream;
|
import zutil.io.StringOutputStream;
|
||||||
|
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
|
|
@ -35,60 +36,61 @@ import java.util.logging.LogRecord;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class CompactLogFormatter extends Formatter{
|
public class CompactLogFormatter extends Formatter{
|
||||||
// The split pattern where the
|
/** The split pattern where the **/
|
||||||
private static final Pattern splitter = Pattern.compile("\n");
|
private static final Pattern splitter = Pattern.compile("\n");
|
||||||
// the stream should print time stamp
|
/** the stream should print time stamp **/
|
||||||
private boolean timeStamp = true;
|
private boolean timeStamp = true;
|
||||||
//The time stamp style
|
/** The time stamp style **/
|
||||||
private SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
private SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
||||||
// If displaying class names are enabled
|
/** If displaying class names are enabled **/
|
||||||
private boolean className = true;
|
private boolean className = true;
|
||||||
// If displaying method names are enabled
|
/** If displaying method names are enabled **/
|
||||||
private boolean methodName = false;
|
private boolean methodName = false;
|
||||||
// Specifies the max length of the longest class name
|
/** Specifies the max length of the longest class name **/
|
||||||
private int max_class_name = 0;
|
private int max_class_name = 0;
|
||||||
// Cache for the class padding
|
/** Cache for the class padding **/
|
||||||
private static HashMap<String,String> padd_cache = new HashMap<String,String>();
|
private static HashMap<String,String> padd_cache = new HashMap<String,String>();
|
||||||
// Date temp file
|
/** Date temp file **/
|
||||||
private Date date = new Date();
|
private Date date = new Date();
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String format(LogRecord record) {
|
public String format(LogRecord record) {
|
||||||
StringBuilder data = new StringBuilder();
|
StringBuilder prefix = new StringBuilder();
|
||||||
|
|
||||||
if( timeStamp ){
|
if( timeStamp ){
|
||||||
date.setTime( record.getMillis() );
|
date.setTime( record.getMillis() );
|
||||||
data.append( dateFormatter.format(date) );
|
prefix.append(dateFormatter.format(date));
|
||||||
data.append(' ');
|
prefix.append(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
switch( record.getLevel().intValue() ){
|
switch( record.getLevel().intValue() ){
|
||||||
case /* SEVERE */ 1000: data.append("[SEVERE] "); break;
|
case /* SEVERE */ 1000: prefix.append("[SEVERE] "); break;
|
||||||
case /* WARNING */ 900 : data.append("[WARNING]"); break;
|
case /* WARNING */ 900 : prefix.append("[WARNING]"); break;
|
||||||
case /* INFO */ 800 : data.append("[INFO] "); break;
|
case /* INFO */ 800 : prefix.append("[INFO] "); break;
|
||||||
case /* CONFIG */ 700 : data.append("[CONFIG] "); break;
|
case /* CONFIG */ 700 : prefix.append("[CONFIG] "); break;
|
||||||
case /* FINE */ 500 : data.append("[FINE] "); break;
|
case /* FINE */ 500 : prefix.append("[FINE] "); break;
|
||||||
case /* FINER */ 400 : data.append("[FINER] "); break;
|
case /* FINER */ 400 : prefix.append("[FINER] "); break;
|
||||||
case /* FINEST */ 300 : data.append("[FINEST] "); break;
|
case /* FINEST */ 300 : prefix.append("[FINEST] "); break;
|
||||||
}
|
}
|
||||||
data.append(' ');
|
prefix.append(' ');
|
||||||
|
|
||||||
if( className ){
|
if( className ){
|
||||||
data.append( paddClassName(record.getSourceClassName()) );
|
prefix.append(paddClassName(record.getSourceClassName()));
|
||||||
}
|
}
|
||||||
if(methodName){
|
if(methodName){
|
||||||
data.append( record.getSourceMethodName() );
|
prefix.append(record.getSourceMethodName());
|
||||||
}
|
}
|
||||||
data.append( ": " );
|
prefix.append(": ");
|
||||||
|
|
||||||
StringBuffer ret = new StringBuffer();
|
StringBuilder ret = new StringBuilder();
|
||||||
if( record.getMessage() != null ){
|
if( record.getMessage() != null ){
|
||||||
String[] array = splitter.split( record.getMessage() );
|
String[] array = splitter.split( record.getMessage() );
|
||||||
for( int i=0; i<array.length ;++i ){
|
for( int i=0; i<array.length ;++i ){
|
||||||
if( i!=0 )
|
if( i!=0 )
|
||||||
ret.append( '\n' );
|
ret.append( '\n' );
|
||||||
if( data.length() > 0 )
|
if( prefix.length() > 0 )
|
||||||
ret.append( data );
|
ret.append( prefix );
|
||||||
ret.append( array[i] );
|
ret.append( array[i] );
|
||||||
}
|
}
|
||||||
ret.append( '\n' );
|
ret.append( '\n' );
|
||||||
|
|
@ -101,8 +103,8 @@ public class CompactLogFormatter extends Formatter{
|
||||||
for( int i=0; i<array.length ;++i ){
|
for( int i=0; i<array.length ;++i ){
|
||||||
if( i!=0 )
|
if( i!=0 )
|
||||||
ret.append( '\n' );
|
ret.append( '\n' );
|
||||||
if( data.length() > 0 )
|
if( prefix.length() > 0 )
|
||||||
ret.append( data );
|
ret.append( prefix );
|
||||||
ret.append( array[i] );
|
ret.append( array[i] );
|
||||||
}
|
}
|
||||||
ret.append( '\n' );
|
ret.append( '\n' );
|
||||||
|
|
@ -151,21 +153,16 @@ public class CompactLogFormatter extends Formatter{
|
||||||
*/
|
*/
|
||||||
private String paddClassName(String source){
|
private String paddClassName(String source){
|
||||||
String tmp = padd_cache.get(source);
|
String tmp = padd_cache.get(source);
|
||||||
if( tmp != null )
|
if(tmp != null && tmp.length() == max_class_name)
|
||||||
return tmp;
|
return tmp;
|
||||||
|
|
||||||
String c_name = source.substring( source.lastIndexOf('.')+1 );
|
String c_name = source.substring( source.lastIndexOf('.')+1 );
|
||||||
if( c_name.length() > max_class_name )
|
if( c_name.length() > max_class_name )
|
||||||
max_class_name = c_name.length();
|
max_class_name = c_name.length();
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder( c_name );
|
c_name += StringUtil.getSpaces(max_class_name - c_name.length());
|
||||||
for( int i=c_name.length(); i<max_class_name; ++i ) {
|
padd_cache.put(source, c_name);
|
||||||
sb.append( ' ' );
|
return c_name;
|
||||||
}
|
|
||||||
tmp = sb.toString();
|
|
||||||
padd_cache.put(source, tmp);
|
|
||||||
return tmp;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
73
src/zutil/log/InputStreamLogger.java
Normal file
73
src/zutil/log/InputStreamLogger.java
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
package zutil.log;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class will log all data that passes through a InputStream.
|
||||||
|
* The logging is done with Javas standard Logger with Level.FINEST.
|
||||||
|
*
|
||||||
|
* Created by Ziver on 2015-10-15.
|
||||||
|
*/
|
||||||
|
public class InputStreamLogger extends InputStream implements StreamLogger.LogCallback {
|
||||||
|
private static final Logger logger = LogUtil.getLogger();
|
||||||
|
|
||||||
|
private InputStream in;
|
||||||
|
private StreamLogger log;
|
||||||
|
|
||||||
|
public InputStreamLogger(InputStream in){
|
||||||
|
this(null, in);
|
||||||
|
}
|
||||||
|
public InputStreamLogger(String prefix, InputStream in){
|
||||||
|
this.in = in;
|
||||||
|
this.log = new StreamLogger(prefix, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean isLoggable(){
|
||||||
|
return logger.isLoggable(Level.FINEST);
|
||||||
|
}
|
||||||
|
public void log(String msg){
|
||||||
|
logger.finest(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
//************** PROXY METHODS ******************
|
||||||
|
public int read() throws IOException{
|
||||||
|
int n = in.read();
|
||||||
|
log.log(n);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
public int read(byte b[]) throws IOException {
|
||||||
|
int n = in.read(b);
|
||||||
|
log.log(b, 0, b.length);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
public int read(byte b[], int off, int len) throws IOException {
|
||||||
|
int n = in.read(b, off, len);
|
||||||
|
log.log(b, off, len);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
public long skip(long n) throws IOException {
|
||||||
|
return in.skip(n);
|
||||||
|
}
|
||||||
|
public int available() throws IOException {
|
||||||
|
return in.available();
|
||||||
|
}
|
||||||
|
public void close() throws IOException {
|
||||||
|
in.close();
|
||||||
|
log.flushLog();
|
||||||
|
}
|
||||||
|
public void mark(int readlimit) {
|
||||||
|
in.mark(readlimit);
|
||||||
|
}
|
||||||
|
public void reset() throws IOException {
|
||||||
|
in.reset();
|
||||||
|
log.clearLog();
|
||||||
|
}
|
||||||
|
public boolean markSupported() {
|
||||||
|
return in.markSupported();
|
||||||
|
}
|
||||||
|
}
|
||||||
59
src/zutil/log/OutputStreamLogger.java
Normal file
59
src/zutil/log/OutputStreamLogger.java
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
package zutil.log;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class will log all data that passes through a InputStream.
|
||||||
|
* The logging is done with Javas standard Logger with Level.FINEST.
|
||||||
|
*
|
||||||
|
* Created by Ziver on 2015-10-15.
|
||||||
|
*/
|
||||||
|
public class OutputStreamLogger extends OutputStream implements StreamLogger.LogCallback {
|
||||||
|
private static final Logger logger = LogUtil.getLogger();
|
||||||
|
|
||||||
|
private OutputStream out;
|
||||||
|
private StreamLogger log;
|
||||||
|
|
||||||
|
|
||||||
|
public OutputStreamLogger(OutputStream out){
|
||||||
|
this(null, out);
|
||||||
|
}
|
||||||
|
public OutputStreamLogger(String prefix, OutputStream out){
|
||||||
|
this.out = out;
|
||||||
|
this.log = new StreamLogger(prefix, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean isLoggable(){
|
||||||
|
return logger.isLoggable(Level.FINEST);
|
||||||
|
}
|
||||||
|
public void log(String msg){
|
||||||
|
logger.finest(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
//************** PROXY METHODS ******************
|
||||||
|
public void write(int b) throws IOException{
|
||||||
|
out.write(b);
|
||||||
|
log.log(b);
|
||||||
|
}
|
||||||
|
public void write(byte b[]) throws IOException {
|
||||||
|
out.write(b, 0, b.length);
|
||||||
|
log.log(b, 0, b.length);
|
||||||
|
}
|
||||||
|
public void write(byte b[], int off, int len) throws IOException {
|
||||||
|
out.write(b, off, len);
|
||||||
|
log.log(b, off, len);
|
||||||
|
}
|
||||||
|
public void flush() throws IOException {
|
||||||
|
out.flush();
|
||||||
|
log.flushLog();
|
||||||
|
}
|
||||||
|
public void close() throws IOException {
|
||||||
|
out.close();
|
||||||
|
log.flushLog();
|
||||||
|
}
|
||||||
|
}
|
||||||
78
src/zutil/log/StreamLogger.java
Normal file
78
src/zutil/log/StreamLogger.java
Normal file
|
|
@ -0,0 +1,78 @@
|
||||||
|
package zutil.log;
|
||||||
|
|
||||||
|
import com.mysql.jdbc.log.Log;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Ziver on 2015-10-15.
|
||||||
|
*/
|
||||||
|
public class StreamLogger {
|
||||||
|
private static final byte DELIMETER = '\n';
|
||||||
|
private static final int MAX_BUFFER_SIZE = 1024;
|
||||||
|
|
||||||
|
|
||||||
|
private LogCallback logger;
|
||||||
|
private String prefix;
|
||||||
|
private StringBuilder buffer;
|
||||||
|
|
||||||
|
|
||||||
|
protected StreamLogger(String prefix, LogCallback logger){
|
||||||
|
if(logger == null)
|
||||||
|
throw new NullPointerException("LogCallback can not be NULL");
|
||||||
|
this.prefix = prefix;
|
||||||
|
this.logger = logger;
|
||||||
|
this.buffer = new StringBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void log(int n){
|
||||||
|
if(n == DELIMETER)
|
||||||
|
flushLog();
|
||||||
|
else
|
||||||
|
buffer.append(n);
|
||||||
|
if(buffer.length() > MAX_BUFFER_SIZE)
|
||||||
|
flushLog();
|
||||||
|
}
|
||||||
|
protected void log(byte[] b, int off, int len){
|
||||||
|
try {
|
||||||
|
if (logger.isLoggable()) {
|
||||||
|
for(int i=0; i<len; ++i){
|
||||||
|
if(b[off+i] == DELIMETER){
|
||||||
|
buffer.append(new String(b, off, i, "UTF-8"));
|
||||||
|
flushLog();
|
||||||
|
off += i;
|
||||||
|
len -= i;
|
||||||
|
i = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(len > 0)
|
||||||
|
buffer.append(new String(b, off, len, "UTF-8"));
|
||||||
|
if(buffer.length() > MAX_BUFFER_SIZE)
|
||||||
|
flushLog();
|
||||||
|
}
|
||||||
|
} catch (UnsupportedEncodingException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void flushLog(){
|
||||||
|
if(prefix != null)
|
||||||
|
logger.log(prefix + ": " + buffer.toString());
|
||||||
|
else
|
||||||
|
logger.log(buffer.toString());
|
||||||
|
clearLog();
|
||||||
|
}
|
||||||
|
protected void clearLog(){
|
||||||
|
buffer.delete(0, buffer.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public interface LogCallback{
|
||||||
|
public boolean isLoggable();
|
||||||
|
public void log(String msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -113,7 +113,13 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
|
||||||
if(root != null){
|
if(root != null){
|
||||||
return (T)readObject(c, null, root);
|
return (T)readObject(c, null, root);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (ClassNotFoundException e) {
|
||||||
|
logger.log(Level.WARNING, null, e);
|
||||||
|
} catch (NoSuchFieldException e) {
|
||||||
|
logger.log(Level.WARNING, null, e);
|
||||||
|
} catch (InstantiationException e) {
|
||||||
|
logger.log(Level.WARNING, null, e);
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
logger.log(Level.WARNING, null, e);
|
logger.log(Level.WARNING, null, e);
|
||||||
} finally {
|
} finally {
|
||||||
objectCache.clear();
|
objectCache.clear();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue