forked from JavaTX/JavaCompilerCore
103 lines
2.3 KiB
Java
103 lines
2.3 KiB
Java
package de.dhbwstuttgart.logger;
|
|
|
|
import java.io.PrintStream;
|
|
import java.util.HashMap;
|
|
import java.util.logging.Handler;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.LogRecord;
|
|
|
|
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;
|
|
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* Logt eine Debug Message, welche zusätzlich einer bestimmten Section zugewiesen wird.
|
|
* Dadurch lässt sich die DEBUG ausgabe übersichtlicher gestalten.
|
|
* @param message
|
|
* @param section
|
|
*/
|
|
public void debug(String message, Section section){
|
|
output(message, Level.FINE);
|
|
}
|
|
|
|
public void debug(String message){
|
|
//output(message, Level.FINE);
|
|
}
|
|
|
|
/**
|
|
* Liefert den Logger mit dem angegebenen Namen.
|
|
* Üblicherweise wird diese Methode mit dem Namen der Klasse aufgerufen, in welcher der Logger tätig ist.
|
|
* @param name - Name der Klasse ( Ermittelbar mittels <Klasse>.class.getName() )
|
|
* @return
|
|
*/
|
|
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);
|
|
/*
|
|
if(output != null){
|
|
output.println(msg);
|
|
}else if(standardOutput != null){
|
|
standardOutput.println(msg);
|
|
}
|
|
*/
|
|
}
|
|
|
|
public void info(String message) {
|
|
output(message, Level.INFO);
|
|
}
|
|
|
|
public void error(String message) {
|
|
output(message, Level.WARNING);
|
|
}
|
|
|
|
/**
|
|
* wird hier null übergeben, so wird sämtliches Logging unterdrückt.
|
|
*/
|
|
public static void setStandardOutput(PrintStream outputStream) {
|
|
Logger.standardOutput = outputStream;
|
|
}
|
|
}
|
|
|
|
class OutputHandler extends Handler{
|
|
|
|
private PrintStream output;
|
|
|
|
public OutputHandler(PrintStream output){
|
|
this.output = output;
|
|
}
|
|
|
|
@Override
|
|
public void publish(LogRecord record) {
|
|
output.println(record.getMessage());
|
|
}
|
|
|
|
@Override
|
|
public void flush() {
|
|
}
|
|
|
|
@Override
|
|
public void close() throws SecurityException {
|
|
}
|
|
}
|