Java logging levels confusion

15,816

Solution 1

I typically use logback to implement logging, which seems a tad better documented. So I would recommend switching to that.

But to answer your question, I think what is happening is that your Logger is configured correctly, but the Handler it's sending its messages to isn't. The default configuration probably attaches a handler with INFO level logging to the root logger.

edit: I wrote a little test program to verify, you indeed need to set the level on the handler attached to the root logger. You can do so like this:

for (Handler handler : Logger.getLogger("").getHandlers()) {
    handler.setLevel(Level.CONFIG);
}
logger.config("config");

Gives as output:

Feb 11, 2011 4:32:14 PM Test main
CONFIG: config

This sets the level for all handlers attached to this. Obviously a better choice would be writing your own options file and explicitly configuring your loggers. A quick google turned up this article on the subject.

You could also try configuring with a properties file on your classpath that reads:

java.util.logging.ConsoleHandler.level=CONFIG

Solution 2

This is the expected behavior.

When you define a level of logs, you will see all the logs of this level, but also the ones linked to higher level.

The order is the following:

  • SEVERE (highest value)
  • WARNING
  • INFO
  • CONFIG
  • FINE
  • FINER
  • FINEST (lowest value)

So in your case, if you define the level to INFO, you will only see SEVERE, WARNING and INFO logs, and not CONFIG messages.


Edit, to answer your corrected question:

Maybe a third party library is used for your Logger class (log4j, slf4j, and so on), and this library defines its own level of log. For example, for log4j, there are only the following levels:

  • DEBUG
  • INFO
  • WARN
  • ERROR
  • FATAL

In such case, the level CONFIG is considered as a INFO level, that explains your current behavior.

Share:
15,816
Hadeel Fouad
Author by

Hadeel Fouad

e-mail: alex2k8.stackoverflow [at] gmail [dot] com

Updated on June 04, 2022

Comments

  • Hadeel Fouad
    Hadeel Fouad almost 2 years

    I set logging level to CONFIG, but don't see messages written on CONFIG level. What I am missing?

    Configuration:

    Logger logger = java.util.logging.Logger.getLogger("xxx");
    logger.setLevel(java.util.logging.Level.CONFIG);
    

    Tests:

    logger.log(java.util.logging.Level.SEVERE, "severe");
    logger.log(java.util.logging.Level.WARNING, "warning");
    logger.log(java.util.logging.Level.INFO, "info");
    logger.log(java.util.logging.Level.CONFIG, "config");
    logger.log(java.util.logging.Level.FINE, "fine");
    logger.log(java.util.logging.Level.FINER, "finer");
    logger.log(java.util.logging.Level.FINEST, "finest");
    

    Output:

    SEVERE: severe
    WARNING: warning
    INFO: info
    
  • Matt Ball
    Matt Ball about 13 years
    The OP did not set the level to INFO, however. He set it to CONFIG. edit okay, there's actually conflicting info in the question. His english says "I set the log level to INFO" but his code says logger.setLevel(java.util.logging.Level.CONFIG);
  • Romain Linsolas
    Romain Linsolas about 13 years
    yes, indeed, it is not clear. I considered the first sentence, not the code just below...
  • Hadeel Fouad
    Hadeel Fouad about 13 years
    Sorry for confusion, fixed the question.
  • Hadeel Fouad
    Hadeel Fouad about 13 years
    @romaintaz, I just created a simple java program, and dropped my experimental code in it. And still see SEVERE, WARNING, INFO. May be default implementation also has different levels?
  • Romain Linsolas
    Romain Linsolas about 13 years
    @alex2k8 Just tried your code in a simple Java code, and indeed, I get the same result as yours...