-1

When should we use the Logger API?

For instance, on a InterruptedException in a Thread - should we something along these lines ?

catch (InterruptedException ex) {
     Logger.getLogger(class1.class.getName()).log(Level.SEVERE, null, ex);
}

Thanks

1
  • 1
    Well it does make sense because I'm new to this, and yes, I already make a research based on Google. It's just a question, to see if someone that has this knowledge, can explain it to me. I dont get why you mark my question as minus 1.
    – thathashd
    Oct 30, 2011 at 10:04

1 Answer 1

3

Logs are used to debug programs and to replace print statements. They should not replace exceptions which need to be handled at a higher level. But they may accompany an exception (for example, if you need to rethrow an exception, the common idiom might be to log the exception to an error log file, and then to let the application crash or rethrow the exception as a runtime exception).

There are basically 3 major uses for logging :

1) To trace execution of an application while you are coding the application up, for example, for the first time (i.e. when you are prototyping with the intent to eventually produce a production app which isn't spitting System.out statements all over the place), and to see the output - while building it, you add log statements (rather than print statements), at level DEBUG. At this time in the life cycle, your application would probably print all logs to the console.

2) To generally see status updates or stream status updates to a user or to a file, so that you can ensure that your application is generally in the right "state" during execution. In this scenario, your loggers will probably have different levels (info, error, debug ....). Error messages might be printed to the console, while others would be dumped to a file, or potentially, ignored entirely.

3) To debug a broken application. In this case, you might change the settings of your logging again to print everything to the console, or maybe, to at least dump all logs to a file, so that you can see what is happening.

On a final note : It is more common to have a Logger factory, which is already centrally configured, rather than simply getting a static logger when you need to log a statement.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.