Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using java.util.logging to log on the console

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

like image 349
Michael Avatar asked Feb 08 '12 18:02

Michael


People also ask

Can you do console log in Java?

In Java, we can use the println() method as console. log() to display log messages to the console.

What is Java Util logging?

In Java, the LogManager is a class that belongs to the java. util. logging package. It keeps track the global logging configuration, creates, and maintains the logger instances. We can also use it to set its own application-specific configuration.


3 Answers

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

handler.setLevel(Level.ALL);
like image 158
Joop Eggen Avatar answered Oct 03 '22 02:10

Joop Eggen


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

logger.addHandler(new StreamHandler(System.out, new SimpleFormatter()))
like image 31
kirilv Avatar answered Oct 03 '22 02:10

kirilv


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:

like image 39
user949300 Avatar answered Oct 03 '22 01:10

user949300