Java logging levels confusion
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.se开发者_Go百科tLevel(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
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
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.
精彩评论