This commit is contained in:
parent
7290dba23a
commit
694e3081bb
2 changed files with 42 additions and 4 deletions
|
|
@ -5,6 +5,7 @@ import java.util.HashMap;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import zutil.MultiPrintStream;
|
import zutil.MultiPrintStream;
|
||||||
|
import zutil.wrapper.StringOutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is a logger class
|
* This is a logger class
|
||||||
|
|
@ -14,13 +15,13 @@ import zutil.MultiPrintStream;
|
||||||
*/
|
*/
|
||||||
public class Logger {
|
public class Logger {
|
||||||
// This is the global log level
|
// This is the global log level
|
||||||
protected static Level global_log_level;
|
protected static Level global_log_level = Level.WARNING;
|
||||||
|
// Class Specific log level
|
||||||
|
protected static HashMap<String, Level> class_log_level;
|
||||||
// The OutputStream
|
// The OutputStream
|
||||||
protected static PrintStream out;
|
protected static PrintStream out;
|
||||||
// The formatter class that formats the output
|
// The formatter class that formats the output
|
||||||
protected static LogFormatter formatter;
|
protected static LogFormatter formatter;
|
||||||
// Class Specific log level
|
|
||||||
protected static HashMap<String, Level> class_log_level;
|
|
||||||
|
|
||||||
// The class that is logging
|
// The class that is logging
|
||||||
private String source;
|
private String source;
|
||||||
|
|
@ -38,7 +39,7 @@ public class Logger {
|
||||||
* @param logging_class is the class that will be displayed as the logger
|
* @param logging_class is the class that will be displayed as the logger
|
||||||
*/
|
*/
|
||||||
public Logger(Class<?> logging_class){
|
public Logger(Class<?> logging_class){
|
||||||
this(logging_class.getSimpleName());
|
this( logging_class.getSimpleName() );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -58,6 +59,20 @@ public class Logger {
|
||||||
if(class_log_level == null)
|
if(class_log_level == null)
|
||||||
class_log_level = new HashMap<String, Level>();
|
class_log_level = new HashMap<String, Level>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the global log level
|
||||||
|
*/
|
||||||
|
public static void setGlobalLogLevel(Level l){
|
||||||
|
global_log_level = l;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets an specific log level for an class
|
||||||
|
*/
|
||||||
|
public static void setClassLogLevel(Class<?> c, Level l){
|
||||||
|
class_log_level.put(c.getSimpleName(), l);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the parent class other than Logger in the stack
|
* @return the parent class other than Logger in the stack
|
||||||
|
|
@ -87,6 +102,18 @@ public class Logger {
|
||||||
Logger.out = out;
|
Logger.out = out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logs the given exception
|
||||||
|
*
|
||||||
|
* @param level is the severity of the exception
|
||||||
|
* @param e is the exception
|
||||||
|
*/
|
||||||
|
public void printStackTrace(Level level, Exception e){
|
||||||
|
StringOutputStream msg = new StringOutputStream();
|
||||||
|
e.printStackTrace( new PrintStream(msg) );
|
||||||
|
log(level, msg.toString());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logs an message based on level
|
* Logs an message based on level
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,17 @@ public class StandardLogFormatter implements LogFormatter{
|
||||||
|
|
||||||
public String format(String source, Level level, String msg) {
|
public String format(String source, Level level, String msg) {
|
||||||
StringBuilder data = new StringBuilder();
|
StringBuilder data = new StringBuilder();
|
||||||
|
|
||||||
|
switch(level.intValue()){
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
if( timeStamp && className ){
|
if( timeStamp && className ){
|
||||||
data.append( getTime() );
|
data.append( getTime() );
|
||||||
data.append( " " );
|
data.append( " " );
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue