Verwendung der readConfiguration-Methode bei Protokollierungsaktivitäten
Um die Protokollierung in einer kleinen Java-Desktop-Anwendung nutzen zu können, versuche ich, die Funktionsweise einiger Methoden zu verstehen. Ich benutze ein sehr dummes und kleines Java-Programm, um sie zu testen.
Insbesondere beim Testen des Verhaltens der LogManager.readConfiguration () -Methode habe ich etwas Seltsames gefunden. In allen Tests liest der LogManager seine Konfiguration aus den Eigenschaftendateien in lib / logging.properties im JRE-Verzeichnis. Zu diesem Zeitpunkt sieht der Inhalt dieser Datei wie folgt aus:
handlers=java.util.logging.ConsoleHandler
myapp2.handlers=java.util.logging.ConsoleHandler
myapp2.MyApp2.handlers=java.util.logging.ConsoleHandler
.level=OFF
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
myapp2.level=WARNING
myapp2.MyApp2.level=INFO
Der Code des Java-Programms ist:
package myapp2;
import java.io.IOException;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class MyApp2 {
private static final Logger LOGGER = Logger.getLogger(MyApp2.class.getPackage().getName());
private static final Logger LOGGER1 = Logger.getLogger(MyApp2.class.getName());
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
LOGGER.severe("severe at MyApp2");
LOGGER.warning("warning at MyApp2");
LOGGER.info("info at MyApp2");
LOGGER1.severe("severe1 at MyApp2");
LOGGER1.warning("warning1 at MyApp2");
LOGGER1.info("info1 at MyApp2");
LOGGER1.setLevel(null);
LOGGER1.setUseParentHandlers(false);
LOGGER1.severe("severe2 at MyApp2");
LOGGER1.warning("warning2 at MyApp2");
LOGGER1.info("info2 at MyApp2");
try {
LogManager.getLogManager().readConfiguration();
} catch (IOException ex) {
System.out.println("I/O Exception found");
} catch (SecurityException ex) {
System.out.println("Error SecurityException found");
}
LOGGER.severe("severe3 at MyApp2");
LOGGER1.severe("severe4 at MyApp2");
}
}
Wenn wir es OHNE den try-catch ausführen, der die readConfiguration () umgibt, funktioniert es wie erwartet und die Ausgabe ist wie folgt:
SEVERE: severe at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
SEVERE: severe at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
WARNING: warning at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
WARNING: warning at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
SEVERE: severe1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
SEVERE: severe1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
SEVERE: severe1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
WARNING: warning1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
WARNING: warning1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
WARNING: warning1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
INFO: info1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
INFO: info1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
INFO: info1 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
SEVERE: severe2 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
WARNING: warning2 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
SEVERE: severe3 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
SEVERE: severe3 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
SEVERE: severe4 at MyApp2 [dc. maig 08 14:45:38 CEST 2013]
Wenn wir jedoch WITH the try-catch ausführen, lautet die Ausgabe:
SEVERE: severe at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
SEVERE: severe at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
WARNING: warning at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
WARNING: warning at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
SEVERE: severe1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
SEVERE: severe1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
SEVERE: severe1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
WARNING: warning1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
WARNING: warning1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
WARNING: warning1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
INFO: info1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
INFO: info1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
INFO: info1 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
SEVERE: severe2 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
WARNING: warning2 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
SEVERE: severe3 at MyApp2 [dc. maig 08 14:46:51 CEST 2013]
Durch Lesen der API der Methode readConfiguration () sollen die Protokolleigenschaften erneut initialisiert und die Protokollierungskonfiguration aus der zuvor genannten Datei erneut gelesen werden. Wenn ja, warum wird schwerwiegend3 nur einmal angezeigt (ich erwarte, dass es aufgrund der beiden im Programm vorhandenen LOGGERs und des Weiterleitungsverhaltens zweimal angezeigt wird) und schwerwiegend4 fehlt (ich erwarte, dass es einmal angezeigt wird)? Kann mir jemand helfen, das zu verstehen?