26

I want simply to log on the console using java.util.Logging:

Logger log = Logger.getLogger("my.logger");
log.setLevel(Level.ALL);
ConsoleHandler handler = new ConsoleHandler();
handler.setFormatter(new SimpleFormatter());
log.addHandler(handler);
log.fine("hello world");

but this prints out nothing. What am I missing?

Thanks

Michael
  • 4,722
  • 6
  • 37
  • 58

3 Answers3

33

Very simple, a logger can have several handlers, with each a different level.

handler.setLevel(Level.ALL);
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • 3
    Which begs the question: just what is the purpose of Logger.setLevel(), if it doesn't set the levels? – user949300 Feb 08 '12 at 19:10
  • 1
    The ConsoleHandler starts with level INFO. The levels are combined. My personal opinion is that the logging API does not provide an _algebra_, a sensible set of combinable operations with well defined semantics. – Joop Eggen Feb 08 '12 at 19:32
  • 1
    the logger is used inside the class, may also be configurated by the class. the handler may be set globally. so you might have different handlers, for output to console and/or file. you definetly have different loggers, e.g. for each class. – benez Sep 03 '15 at 13:15
  • @kewlbfy right: one picks a specific logger for usage, and then one dispatch to plural handlers, like for file, console, email, ticket system or whatever. – Joop Eggen Sep 03 '15 at 13:45
12

Logging on the standard System.out stream could be easily done by adding a StreamHandler handler:

logger.addHandler(new StreamHandler(System.out, new SimpleFormatter()))
kirilv
  • 1,373
  • 1
  • 18
  • 24
9

I'm no expert on java logging, but if you change log.fine() to log.info() it will print. There's something fishy about fine - in practice, I never used it. Hopefully somebody who knows more can answer that.

ADDED: Yes, fine is special. I found an earlier SO answer for this:

Community
  • 1
  • 1
user949300
  • 15,364
  • 7
  • 35
  • 66