4

I am using java.util.logging.Logger and I want to restrict the log level to SEVERE, but he does not respect that and logs everything. What is 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");
}

Both the message 1 ("Teste1") and message 2 ("Test2") are being logged. How do I restrict the log level to SEVERE for only the second message ("Test2) appears?

2
  • Works on my machine (not using a logging.properties file). Commented Sep 3, 2012 at 16:01
  • Strange. On my machine appears all logs at any level. Commented Sep 3, 2012 at 16:03

2 Answers 2

1

I believe that you either have not configured the log level correctly in logging.properties file or have not set system property java.util.logging.config.file when running your application.

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

Sign up to request clarification or add additional context in comments.

3 Comments

Are you saying there is nothing you can do at runtime to workaround this?
It's not that I'm using a file logging.properties. I would not use this file logging.properties. I'm trying to set the information through the code.
I would do it without the file logging.properties
0

David, It also works fine in my machine without log4j.properties. Not sure but problem might be in jar you are using. It's stopping you to set log level to 'SEVERE'. Please use the following dependency. It will definitely work.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.