16.6.3 Logging

As mentioned in 16.6.2, Synchronization, the Java I/O classes use synchronization. Hence System.out.println() should not be used for logging purposes. If a lot of output using stdout is generated by an application in a UNIX environment, the overhead can be avoided by redirecting stdout to /dev/null in the production environment. However, a better approach is to use a multithreaded logging library such as the WebSphere logging facilities or Log4J (http://jakarta.apache.org/log4j/). In addition to providing better performance due to their multithreaded implementations, these libraries allow logging statements to be defined at a particular level, which can be dynamically changed at runtime. Thus the amount of logging in production environments can be reduced in comparison to development and test environments without requiring code changes, improving the performance of the application. It is also good practice to guard log statements so that the parameters are not evaluated if the logging level is not on. The use of guard statements is shown in Example 16-2.

Example 16-2 Use of guard statements for logging

if (Log.isLogging(Log.WARN) {
Log.log(LOG.WARN, "This is a warning");

}


Next