165 lines
3.9 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.ArrayList;
import java.util.Date;
2014-10-09 12:01:16 +02:00
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 LoggerConfiguration standardConfiguration = null;
2014-10-09 12:01:16 +02:00
private static final HashMap<String, Logger> LOGGER_DIRECTORY = new HashMap<>();
private String name;
2014-11-04 13:47:05 +01:00
private final HashMap<Section, java.util.logging.Logger> logger;
private static final LogHistory LOG_HISTORY = new LogHistory();
2014-11-04 13:47:05 +01:00
protected Logger(String name, LoggerConfiguration config) {
2014-10-09 12:01:16 +02:00
this.name = name;
2014-11-04 13:47:05 +01:00
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);
});
}
2014-10-09 12:01:16 +02:00
}
2015-02-06 15:41:01 +01:00
/**
* Logt eine Debug Message, welche zusätzlich einer bestimmten Section zugewiesen wird.
* Dadurch ¤sst sich die DEBUG ausgabe übersichtlicher gestalten.
* @param message
* @param section
*/
public void debug(String message, Section section){
2014-11-04 13:47:05 +01:00
output(message, Level.FINE, section);
2014-10-09 12:01:16 +02:00
}
/**
* Liefert den Logger mit dem angegebenen Namen.
* Üblicherweise wird diese Methode mit dem Namen der Klasse aufgerufen, in welcher der Logger ¤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{
2014-11-04 13:47:05 +01:00
ret = new Logger(name, standardConfiguration);
2014-10-09 12:01:16 +02:00
LOGGER_DIRECTORY.put(name, ret);
}
return ret;
}
2014-11-04 13:47:05 +01:00
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));
2014-11-04 13:47:05 +01:00
if(logger.containsKey(section)){
java.util.logging.Logger log = logger.get(section);
log.log(logLevel, msg);
}
2014-10-09 12:01:16 +02:00
}
2014-11-04 13:47:05 +01:00
public void info(String message, Section s) {
output(message, Level.INFO, s);
2014-10-09 12:01:16 +02:00
}
2014-11-04 13:47:05 +01:00
public void error(String message, Section s) {
output(message, Level.WARNING, s);
2014-10-09 12:01:16 +02:00
}
/**
* wird hier null übergeben, so wird ¤mtliches Logging unterdrückt.
2014-10-09 12:01:16 +02:00
*/
2014-11-04 13:47:05 +01:00
public static void setStandardConfiguration(LoggerConfiguration config) {
Logger.standardConfiguration = config;
2014-10-09 12:01:16 +02:00
}
2014-11-04 13:47:05 +01:00
public static String getWholeLog(){
String ret = "";
Logger.LOG_HISTORY.sort((log1, log2)->log1.timestamp.compareTo(log2.timestamp));
ret += Logger.LOG_HISTORY.toString();
return ret;
}
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
}
class LogHistory extends ArrayList<LogLine>{
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 "";
}
}