Discussion:
java.util.logging.Logger setLevel(...) isn't working as I expect
(too old to reply)
Cian
2005-09-02 14:36:55 UTC
Permalink
How does Logger work? How can I set the level of logs that appear?
I thought I called getLogger, set the Level and it should work.
However when I run the following code:


import java.util.logging.*;
public class TestLogger {
private static Logger logger = Logger.getLogger("cian");

public static void main(String args[]) {
Level logLevel;
logger.setLevel(Level.FINER);
System.out.println(logger.getLevel());
logger.severe("7\tsevere");
logger.warning("6\twarning");
logger.info("5\tinfo");
logger.config("4\tconfig");
logger.fine("3\tfine");
logger.finer("2\tfiner");
logger.finest("1\tfinest");
}
}


I get:
FINER
02-Sep-2005 15:32:27 TestLogger main
SEVERE: 7 severe
02-Sep-2005 15:32:28 TestLogger main
WARNING: 6 warning
02-Sep-2005 15:32:28 TestLogger main
INFO: 5 info

What happened my logger.config, logger.fine and logger.finer?
I expect to see them too (but not the logger.finest).

Any help would be appreciated.

Thanks,
Cian
Oliver Wong
2005-09-02 16:58:12 UTC
Permalink
Post by Cian
How does Logger work? How can I set the level of logs that appear?
I thought I called getLogger, set the Level and it should work.
import java.util.logging.*;
public class TestLogger {
private static Logger logger = Logger.getLogger("cian");
public static void main(String args[]) {
Level logLevel;
logger.setLevel(Level.FINER);
System.out.println(logger.getLevel());
logger.severe("7\tsevere");
logger.warning("6\twarning");
logger.info("5\tinfo");
logger.config("4\tconfig");
logger.fine("3\tfine");
logger.finer("2\tfiner");
logger.finest("1\tfinest");
}
}
FINER
02-Sep-2005 15:32:27 TestLogger main
SEVERE: 7 severe
02-Sep-2005 15:32:28 TestLogger main
WARNING: 6 warning
02-Sep-2005 15:32:28 TestLogger main
INFO: 5 info
What happened my logger.config, logger.fine and logger.finer?
I expect to see them too (but not the logger.finest).
Any help would be appreciated.
Yeah, I got tripped up here too. There are two locations where you have
to set the logging level. One is on the logger itself, and the other is on
the handler. I believe that you want to get the root logger
(Logger.getLogger("")), and then get its handler, and then set the level on
that handler as well.

These might not be the exact steps as I'm trying to recall the solution
off the top of my head. Let me know whether or not this worked for you.

- Oliver
Cian
2005-09-06 13:55:35 UTC
Permalink
"Cian" wrote in message
Post by Cian
How does Logger work? How can I set the level of logs that appear?
I thought I called getLogger, set the Level and it should work.
[snip]
Yeah, I got tripped up here too. There are two locations where you have
to set the logging level. One is on the logger itself, and the other is on
the handler. I believe that you want to get the root logger
(Logger.getLogger("")), and then get its handler, and then set the level on
that handler as well.
Oliver,

That now works, and shows all levels to FINER.

Thanks,

Cian

I've included the code for anyone else who needs it: TestLogger.java

import java.util.logging.*;

public class TestLogger {

private static Logger logger = Logger.getLogger("cian");

public static void main(String args[]) {
Logger.getLogger("").getHandlers()[0].setLevel(Level.FINER);

logger.setLevel(Level.FINER);

logger.severe("7\tsevere");
logger.warning("6\twarning");
logger.info("5\tinfo");
logger.config("4\tconfig");
logger.fine("3\tfine");
logger.finer("2\tfiner");
logger.finest("1\tfinest");
}
}

Loading...