package de.dhbwstuttgart.logger; import java.io.PrintStream; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; public class Logger { private static LoggerConfiguration standardConfiguration = null; private static final HashMap LOGGER_DIRECTORY = new HashMap<>(); private String name; private final HashMap logger; private static final LogHistory LOG_HISTORY = new LogHistory(); protected Logger(String name, LoggerConfiguration config) { this.name = name; this.logger = new HashMap<>(); if(config != null){ config.forEach((s,o)->{ java.util.logging.Logger log = java.util.logging.Logger.getLogger( name ); log.setLevel(Level.FINE); log.addHandler(new OutputHandler(o)); logger.put(s, log); }); } } /** * 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, section); } /** * 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 .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, standardConfiguration); LOGGER_DIRECTORY.put(name, ret); } return ret; } public static SectionLogger getSectionLogger(String name, Section s) { Logger ret; if(LOGGER_DIRECTORY.containsKey(name)){ ret = LOGGER_DIRECTORY.get(name); }else{ ret = new Logger(name, standardConfiguration); LOGGER_DIRECTORY.put(name, ret); } return new SectionLogger(ret,s); } protected void output(String msg , Level logLevel, Section section){ Logger.LOG_HISTORY.add(new LogLine(msg, this.name, section, logLevel)); if(logger.containsKey(section)){ java.util.logging.Logger log = logger.get(section); log.log(logLevel, msg); } } public void info(String message, Section s) { output(message, Level.INFO, s); } public void error(String message, Section s) { output(message, Level.WARNING, s); } /** * wird hier null übergeben, so wird sämtliches Logging unterdrückt. */ public static void setStandardConfiguration(LoggerConfiguration config) { Logger.standardConfiguration = config; } public static String getWholeLog(){ String ret = ""; Logger.LOG_HISTORY.sort((log1, log2)->log1.timestamp.compareTo(log2.timestamp)); ret += Logger.LOG_HISTORY.toString(); return ret; } } 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 { } } class LogHistory extends ArrayList{ private static final long serialVersionUID = -1785228323497318261L; @Override public String toString(){ String ret = ""; for(LogLine l : this){ ret += l.toString() + "\n"; } return ret; } } class LogLine { Date timestamp; String message; String name; Section section; Level level; LogLine(String msg, String loggerName, Section section, Level logLevel){ this.timestamp = new Date(); this.message = msg; this.name = loggerName; this.section = section; this.level = logLevel; } public String toString(){ String ret = ""; ret += name + ": "; ret += message; ret += " - " + section.name(); return ret; } public String toJSON(){ return ""; } }