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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.