|
|
|
|
|
|
|
|
|
|
|
package aufgabe8_grafik2d.util; |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license |
|
|
|
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
import java.io.InputStream; |
|
|
|
|
|
import java.time.LocalDateTime; |
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
import java.util.Properties; |
|
|
|
|
|
import java.util.Set; |
|
|
|
|
|
//import java.util.Properties; |
|
|
|
|
|
import java.util.logging.*; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Klasse zur eigens konfigurierten Log-Ausgabe in der Console |
|
|
|
|
|
* @author ahrens |
|
|
|
|
|
*/ |
|
|
|
|
|
public class OhmLogger |
|
|
|
|
|
{ |
|
|
|
|
|
private static final String LOGGER_NAME = "OhmLogger"; |
|
|
|
|
|
private static Logger lg = null; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* privater Konstrukter -> Singleton |
|
|
|
|
|
*/ |
|
|
|
|
|
private OhmLogger() |
|
|
|
|
|
{ |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Public Method zum Erstellen des Loggers und zum Aufruf der Methode |
|
|
|
|
|
* zum Initialisieren |
|
|
|
|
|
* @return Referenz auf Logger |
|
|
|
|
|
*/ |
|
|
|
|
|
public static Logger getLogger() |
|
|
|
|
|
{ |
|
|
|
|
|
if (lg == null) |
|
|
|
|
|
{ |
|
|
|
|
|
lg = Logger.getLogger(LOGGER_NAME); |
|
|
|
|
|
initLogger(); |
|
|
|
|
|
} |
|
|
|
|
|
return lg; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Methode zum Initialisieren, Suchen der Konfigurationsdatei, Anlegen eines |
|
|
|
|
|
* neuen Console Handlers, Löschen der bisherigen Standard Handler und |
|
|
|
|
|
* Einfügen des eigenen Handler, Setzen des Levels, je nach Properties |
|
|
|
|
|
*/ |
|
|
|
|
|
private static void initLogger() |
|
|
|
|
|
{ |
|
|
|
|
|
try (InputStream configFile = OhmLogger.class.getClassLoader().getResourceAsStream("config.properties")) { |
|
|
|
|
|
if (configFile != null) { |
|
|
|
|
|
Properties properties = new Properties(); |
|
|
|
|
|
properties.load(configFile); |
|
|
|
|
|
java.util.logging.Level classLogLevel = java.util.logging.Level.parse(properties.getProperty("log.level")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ConsoleHandler ch = new ConsoleHandler(); |
|
|
|
|
|
ch.setFormatter(new OhmFormatter()); |
|
|
|
|
|
lg.setUseParentHandlers(false); |
|
|
|
|
|
lg.getHandlers(); |
|
|
|
|
|
lg.addHandler(ch); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lg.setLevel(classLogLevel); |
|
|
|
|
|
|
|
|
|
|
|
String logDirectory = properties.getProperty("log.directory"); |
|
|
|
|
|
String logFileName = properties.getProperty("log.filename"); |
|
|
|
|
|
|
|
|
|
|
|
FileHandler fileHandler = new FileHandler(logDirectory + "/" + logFileName, true); |
|
|
|
|
|
fileHandler.setFormatter(new OhmFormatter()); |
|
|
|
|
|
lg.addHandler(fileHandler); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
System.err.println("Unable to find config.properties file. OhmLogger will use default settings."); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (IOException | SecurityException e) { |
|
|
|
|
|
System.err.println("Error configuring OhmLogger: " + e.getMessage()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Klasse zum Setzen des Formates des Auszugebenden Log-Strings |
|
|
|
|
|
* @author ahren |
|
|
|
|
|
*/ |
|
|
|
|
|
class OhmFormatter extends Formatter |
|
|
|
|
|
{ |
|
|
|
|
|
@Override |
|
|
|
|
|
public String format(LogRecord record) |
|
|
|
|
|
{ |
|
|
|
|
|
String logline = "| "; |
|
|
|
|
|
|
|
|
|
|
|
LocalDateTime ldt = LocalDateTime.now(); |
|
|
|
|
|
logline += ldt.toString(); |
|
|
|
|
|
logline += " | " + record.getLevel(); |
|
|
|
|
|
logline += " | " + record.getSourceClassName(); |
|
|
|
|
|
logline += " | " + record.getMessage(); |
|
|
|
|
|
logline += " |\n"; |
|
|
|
|
|
|
|
|
|
|
|
return logline; |
|
|
|
|
|
} |
|
|
|
|
|
} |