0

(I' new to Java, and I read java.util.logging: how to set level by logger package (or prefix)? already. As it couldn't answer my question, here it is)

I'm developing a system that has private static final Logger log = Logger.getLogger(XXX.class.getName()); attributes in several classes (XXX being the corresponding class).

I use log.setLevel(level) with different levels, but all the log objects seem to work at the same log level. Printing the log itself, seems to indicate that the individual log objects are actually the same. I don't understand why.

Debug output is like this:

[CONFIG ]...Parser init: java.util.logging.Logger@6bc7c054 logging level FINE
[CONFIG ]...Tokenizer init: java.util.logging.Logger@6bc7c054 logging level CONFIG

And still the Parser class logs at level CONFIG...

jmehrens
  • 10,580
  • 1
  • 38
  • 47
U. Windl
  • 3,480
  • 26
  • 54
  • Include a sample program and a sample logging.properties. It is hard to understand your output above without being able to read source code that produced it. – jmehrens Apr 19 '18 at 22:54
  • I wanted to keep things as compact as possible. The system consists of something like 20 classes, and it wouldn't make the problem more clear, I guess. Maybe the other way round: Present a simple example that does what I want. – U. Windl Apr 20 '18 at 00:24
  • [When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. Click this comment to find out how to provide what we need to help you.](https://stackoverflow.com/help/mcve) –  Apr 23 '18 at 15:19
  • Please read [How do I ask a question that is answerable?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. –  Apr 23 '18 at 15:20

2 Answers2

1

Printing the log itself, seems to indicate that the individual log objects are actually the same. I don't understand why.

Parser and Tokenizer are calling Logger.getLogger(XXX.class.getName()); with the same name XXX class. Modify your code example to print the name of the logger.

Debug output is like this

This is where a Minimal, Complete, and Verifiable example helps me. Levels are used to qualify messages and levels are used to filter messages. If you don't include at least the code that produced your debug output it is hard to tell what the meaning is for each level listed.

jmehrens
  • 10,580
  • 1
  • 38
  • 47
  • If you read the question, you'll see that `XXX` is a placeholder for the corresponding classes, and (I guess) you can see from the output (like "...Parser init..." and "...Tokenizer init...") that the loggers do use different names. – U. Windl Apr 23 '18 at 09:32
  • Actually you were on the right track: Due to the inability to get the name of the *current* class, I had made a copy and paste error, effectively using one class name twice. Unfortunately all the log messages still did output the correct class, so the bug remained undetected until today. So the credits go to you. – U. Windl Apr 23 '18 at 10:10
0

You appear to be confusing the Logger's filtering level and a message's logging level.

Each logger has a logging level that acts as a filter for messages; messages that are logged at a level which is lower than the filtering level of the logger are ignored.

Here is an example (not actual code):

Logger myLogger = Logger.getLogger("somename");
myLogger.setLevel(FINE);

myLogger.fine("fine grained log message");
myLogger.finest("finest level of logging");
myLogger.info("info level message");
myLogger.fine("second fine message");

The "code" above will produce the following messages in the log file:

fine grained log message
info level message
second fine message

Note that the message "finest level of logging" will not appear in the log because FINEST level is lower than FINE and is thus filtered out by the logger.

DwB
  • 37,124
  • 11
  • 56
  • 82
  • I understand your example, but your example does not apply to my problem: I intend to use different logger objects, but it seems I opnly get one logger object (as should be obvious from the `@6bc7c054` output). And using `Level.ALL` for the handler does not fix the problem I described. – U. Windl Apr 23 '18 at 09:42