2014-10-09 12:01:16 +02:00
|
|
|
|
package de.dhbwstuttgart.logger;
|
|
|
|
|
|
|
|
|
|
import java.io.PrintStream;
|
|
|
|
|
import java.util.HashMap;
|
2014-10-09 17:38:10 +02:00
|
|
|
|
import java.util.logging.Level;
|
2014-10-09 12:01:16 +02:00
|
|
|
|
|
|
|
|
|
public class Logger {
|
|
|
|
|
|
|
|
|
|
private static PrintStream standardOutput;
|
|
|
|
|
private static final HashMap<String, Logger> LOGGER_DIRECTORY = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
private String name;
|
2014-10-09 17:38:10 +02:00
|
|
|
|
private final java.util.logging.Logger log;
|
|
|
|
|
|
2014-10-09 12:01:16 +02:00
|
|
|
|
private Logger(String name, PrintStream output) {
|
|
|
|
|
this.name = name;
|
2014-10-09 17:38:10 +02:00
|
|
|
|
this.log = java.util.logging.Logger.getLogger( name );
|
|
|
|
|
if(output != null)log.addHandler(new OutputHandler(output));
|
|
|
|
|
log.setLevel(Level.FINE);
|
2014-10-09 12:01:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-09 17:38:10 +02:00
|
|
|
|
/**
|
|
|
|
|
* Logt eine Debug Message, welche zus<EFBFBD>tzlich einer bestimmten Section zugewiesen wird.
|
|
|
|
|
* Dadurch l<EFBFBD>sst sich die DEBUG ausgabe <EFBFBD>bersichtlicher gestalten.
|
|
|
|
|
* @param message
|
|
|
|
|
* @param section
|
|
|
|
|
*/
|
|
|
|
|
public void debug(String message, Section section){
|
|
|
|
|
output(message, Level.FINE);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-09 12:01:16 +02:00
|
|
|
|
public void debug(String message){
|
2014-10-09 17:38:10 +02:00
|
|
|
|
//output(message, Level.FINE);
|
2014-10-09 12:01:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-09 17:38:10 +02:00
|
|
|
|
/**
|
|
|
|
|
* Liefert den Logger mit dem angegebenen Namen.
|
|
|
|
|
* <EFBFBD>blicherweise wird diese Methode mit dem Namen der Klasse aufgerufen, in welcher der Logger t<EFBFBD>tig ist.
|
|
|
|
|
* @param name - Name der Klasse ( Ermittelbar mittels <Klasse>.class.getName() )
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2014-10-09 12:01:16 +02:00
|
|
|
|
public static Logger getLogger(String name) {
|
|
|
|
|
Logger ret;
|
|
|
|
|
if(LOGGER_DIRECTORY.containsKey(name)){
|
|
|
|
|
ret = LOGGER_DIRECTORY.get(name);
|
|
|
|
|
}else{
|
|
|
|
|
ret = new Logger(name, standardOutput);
|
|
|
|
|
LOGGER_DIRECTORY.put(name, ret);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-09 17:38:10 +02:00
|
|
|
|
private void output(String msg , Level logLevel){
|
|
|
|
|
log.log(logLevel, msg);
|
|
|
|
|
/*
|
2014-10-09 12:01:16 +02:00
|
|
|
|
if(output != null){
|
|
|
|
|
output.println(msg);
|
|
|
|
|
}else if(standardOutput != null){
|
|
|
|
|
standardOutput.println(msg);
|
|
|
|
|
}
|
2014-10-09 17:38:10 +02:00
|
|
|
|
*/
|
2014-10-09 12:01:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-09 17:38:10 +02:00
|
|
|
|
public void info(String message) {
|
|
|
|
|
output(message, Level.INFO);
|
2014-10-09 12:01:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-09 17:38:10 +02:00
|
|
|
|
public void error(String message) {
|
|
|
|
|
output(message, Level.WARNING);
|
2014-10-09 12:01:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* wird hier null <EFBFBD>bergeben, so wird s<EFBFBD>mtliches Logging unterdr<EFBFBD>ckt.
|
|
|
|
|
*/
|
|
|
|
|
public static void setStandardOutput(PrintStream outputStream) {
|
|
|
|
|
Logger.standardOutput = outputStream;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|