103 lines
2.3 KiB
Java
Raw Normal View History

2014-10-09 12:01:16 +02:00
package de.dhbwstuttgart.logger;
import java.io.PrintStream;
import java.util.HashMap;
2014-10-13 10:39:14 +02:00
import java.util.logging.Handler;
import java.util.logging.Level;
2014-10-13 10:39:14 +02:00
import java.util.logging.LogRecord;
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;
private final java.util.logging.Logger log;
2014-10-09 12:01:16 +02:00
private Logger(String name, PrintStream output) {
this.name = name;
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
}
/**
* 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){
//output(message, Level.FINE);
2014-10-09 12:01:16 +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;
}
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 12:01:16 +02:00
}
public void info(String message) {
output(message, Level.INFO);
2014-10-09 12:01:16 +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;
}
2014-10-13 10:39:14 +02:00
}
class OutputHandler extends Handler{
private PrintStream output;
public OutputHandler(PrintStream output){
this.output = output;
}
2014-10-09 12:01:16 +02:00
2014-10-13 10:39:14 +02:00
@Override
public void publish(LogRecord record) {
output.println(record.getMessage());
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
2014-10-09 12:01:16 +02:00
}