Lesson 34Java Logging API

Every Java program performs various functions during its life cycle—for example, it opens files, connects to databases or remote services, or it performs some calculations. Often software developers need to print application-specific messages from various parts of the program. Sometimes developers need to confirm that a program visited a certain method or print the results of intermediate calculations.

Proper reporting of runtime errors is even more important. You may want to print not only the error messages, but also the messages that inform about the control flow of the program (for example, an order has been placed or cancelled). If your Java application server won’t start, the first place to check is its log files, which may have a description of an issue, or at least you can see at which stage the launch process stopped working. 

Until now, in most of the code samples in this book (except graphical user interface applications) I’ve used the tried and true method  System.out.println(),  but this approach has several issues: 1. It’s not easy to “turn off” multiple informational println() calls when the application goes into quality assurance (QA) or production. You need to find and comment out these statements that are sprinkled through your application code. And if a bug is reported in the production application, you need to uncomment several println() statements and potentially add more of them.

2. There is no trace of what’s happened in the ...

Get Java Programming 24-Hour Trainer, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.