|
|
|
|
|
|
|
|
|
|
|
|
|
|
package bandit.util; |
|
|
package bandit.util; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
import java.io.InputStream; |
|
|
import java.time.LocalDateTime; |
|
|
import java.time.LocalDateTime; |
|
|
|
|
|
import java.util.Properties; |
|
|
import java.util.logging.*; |
|
|
import java.util.logging.*; |
|
|
/** |
|
|
/** |
|
|
* |
|
|
|
|
|
* @author le |
|
|
|
|
|
|
|
|
* Klasse zur eigens konfigurierten Log-Ausgabe in der Console |
|
|
|
|
|
* @author ahrens |
|
|
*/ |
|
|
*/ |
|
|
public class OhmLogger |
|
|
public class OhmLogger |
|
|
{ |
|
|
{ |
|
|
|
|
|
private static final String LOGGER_NAME = "OhmLogger"; |
|
|
private static Logger lg = null; |
|
|
private static Logger lg = null; |
|
|
|
|
|
|
|
|
private OhmLogger() //Singelton-Patter -> privater ctor -> anlegen nur über klassenmethode möglich |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 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() |
|
|
public static Logger getLogger() |
|
|
{ |
|
|
{ |
|
|
if (lg == null) |
|
|
if (lg == null) |
|
|
{ |
|
|
{ |
|
|
lg = Logger.getLogger("OhmLogger"); |
|
|
|
|
|
|
|
|
lg = Logger.getLogger(LOGGER_NAME); |
|
|
initLogger(); |
|
|
initLogger(); |
|
|
} |
|
|
} |
|
|
return lg; |
|
|
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() |
|
|
private static void initLogger() |
|
|
{ |
|
|
{ |
|
|
// FileHandler fh = new FileHandler(); |
|
|
|
|
|
ConsoleHandler ch = new ConsoleHandler(); |
|
|
|
|
|
ch.setFormatter(new OhmFormatter()); |
|
|
|
|
|
lg.addHandler(ch); |
|
|
|
|
|
|
|
|
try (InputStream configFile = OhmLogger.class.getClassLoader().getResourceAsStream("config.properties")) { |
|
|
|
|
|
if (configFile != null) { |
|
|
|
|
|
ConsoleHandler ch = new ConsoleHandler(); |
|
|
|
|
|
ch.setFormatter(new OhmFormatter()); |
|
|
|
|
|
lg.setUseParentHandlers(false); |
|
|
|
|
|
lg.getHandlers(); |
|
|
|
|
|
lg.addHandler(ch); |
|
|
|
|
|
|
|
|
|
|
|
lg.setLevel(Level.parse(System.getProperty("log.level", "INFO"))); |
|
|
|
|
|
|
|
|
|
|
|
// Lese spezifische Level für einzelne Klassen aus der Properties-Datei |
|
|
|
|
|
// Properties properties = new Properties(); |
|
|
|
|
|
// properties.load(configFile); |
|
|
|
|
|
// |
|
|
|
|
|
// for (String key : properties.stringPropertyNames()) { |
|
|
|
|
|
// if (key.startsWith("log.level.")) { |
|
|
|
|
|
// String className = key.substring("log.level.".length()); |
|
|
|
|
|
// Logger classLogger = Logger.getLogger(className); |
|
|
|
|
|
// java.util.logging.Level classLogLevel = java.util.logging.Level.parse(properties.getProperty(key)); |
|
|
|
|
|
// classLogger.setLevel(classLogLevel); |
|
|
|
|
|
// } |
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
} 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()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
class OhmFormatter extends SimpleFormatter |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Klasse zum Setzen des Formates des Auszugebenden Log-Strings |
|
|
|
|
|
* @author ahren |
|
|
|
|
|
*/ |
|
|
|
|
|
class OhmFormatter extends Formatter |
|
|
{ |
|
|
{ |
|
|
@Override |
|
|
@Override |
|
|
public String format(LogRecord record) |
|
|
public String format(LogRecord record) |
|
|
{ |
|
|
{ |
|
|
String logline = ""; |
|
|
|
|
|
|
|
|
String logline = "| "; |
|
|
|
|
|
|
|
|
LocalDateTime ldt = LocalDateTime.now(); |
|
|
LocalDateTime ldt = LocalDateTime.now(); |
|
|
logline += ldt.toString(); |
|
|
logline += ldt.toString(); |
|
|
logline += ";" + record.getMessage(); |
|
|
|
|
|
logline += "\n"; |
|
|
|
|
|
|
|
|
logline += " | " + record.getLevel(); |
|
|
|
|
|
logline += " | " + record.getSourceClassName(); |
|
|
|
|
|
logline += " | " + record.getMessage(); |
|
|
|
|
|
logline += " |\n"; |
|
|
|
|
|
|
|
|
return logline; |
|
|
return logline; |
|
|
} |
|
|
} |