Java.util.logging.Logger does not respect java.util.logging.Level

I use java.util.logging.Logger and I want to limit the level of the SEVERE log, but it does not respect this and writes everything. What's wrong?

private static final Logger log = Logger.getLogger(MyClass.class.getName()); private Handler fileHandler = null; public static void myMethod(){ fileHandler = new FileHandler("file", 1000000, 1, true); log.setLevel(Level.SEVERE); fileHandler.setLevel(Level.SEVERE); SimpleFormatter formatter = new SimpleFormatter(); fileHandler.setFormatter(formatter); log.addHandler(fileHandler); log.log(Level.INFO, "Test1"); log.log(Level.SEVERE, "Test2"); } 

Message 1 ("Teste1") and message 2 ("Test2") are recorded. How to limit the level of the SEVERE log to only the second message ("Test2")?

+4
source share
2 answers

I believe that you either incorrectly configured the log level in the logging.properties file or did not set the java.util.logging.config.file system property when starting the application.

use -Djava.util.logging.config.file=PATH TO YOUR logging.properties FILE when starting the application.

+1
source

David, it also works great on my machine without log4j.properties. Not sure, but the problem may be in the bank you are using. This stops you to set the log level to "SEVERE". Use the following dependency. This will definitely work.

 <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> 
0
source

Source: https://habr.com/ru/post/1432111/


All Articles