0

in the following code example the handler does not repect the call to setLevel.

Logger globalLogger = Logger.getLogger("");

Handler handler = new LogMailHandler();

handler.setLevel(Level.SEVERE);

globalLogger.addHandler(handler);

Logger local = Logger.getLogger(LogMailHandlerTest.class.getName());

local.severe("Test message severe...");
local.info("Test message info...");

LogMailHandler is defined as follows:

public class LogMailHandler extends Handler {
    @Override
    public void publish(LogRecord pRecord) {
        System.out.println("Error registered..." + pRecord.getLevel().getName());
    }

    @Override
    public void flush() {
    }

    @Override
    public void close() throws SecurityException {
    }

}

The output is:

Jan 19, 2015 5:20:33 PM com.idmedia.fts.exchange.helper.LogMailHandlerTest main
SEVERE: Test message severe...
Error registered...SEVERE
Jan 19, 2015 5:20:33 PM com.idmedia.fts.exchange.helper.LogMailHandlerTest main
INFO: Test message info...
Error registered...INFO

In my opinion the "Error registered...INFO" should not be there since the level of the handler was set to SEVERE.

Any suggestions?

1 Answer 1

1

You must test the LogRecord on publish method with boolean isLoggable(LogRecord) before printing on console. This method tests, among other things, if the LogRecord level is higher that the minimum (in your case SEVERE)

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

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.