- When logging, as there is an increase in file IO operations, it affects the performance of the application very badly
- The following best practices should be implemented for effective Java logging:
– Log DEBUG messages inside isDebugEnabled() block as given below:

– This practice saves a lot of string concatenation activity if the code is running in the production environment with production logging level instead of debug logging level
- Make use of good Java logging frameworks like java.util.logging or log4j
– For example, by using Log4j the logging levels can be changed without restarting the application and that gives a great advantage in a production or controlled environment
Secured Practices in Logging (Cont’d)
- Do not log sensitive data when logging in a production environment. Logging sensitive information may cause security risks.
Example: e-mail address, phone numbers and account numbers should not be logged - In server applications, all bugs and errors can be tracked only by using Java logs and too many logs will affect the application; therefore, select the appropriate logging level for every logging message
- Ensure that the format of the Java logging in the specified Java logger is included. In the case of java.util.logging API, the properties file should include the logging formatter. While printing logs the thread name and the Java class name should be specified
- Log messages consistently and the messages must be informative. The data should be printed with the message wherever required
- Use code level prefix when logging information (e.g. database code or session code etc.) so that it will be clear to identify which part of the code is printing the log message
Module Summary
- Exception and error occur when an unexpected or abnormal event takes place during the execution of the application
- Exceptions in Java are handled using ‘try-catch-finally’ blocks
- java.lang.InterruptedException is thrown when a thread is interrupted while sleeping or waiting
- Use of return, break, continue or throw statements is restricted in a finally block
- System.exit() when invoked can even terminate the Java Virtual Machine (JVM) resulting in the termination currently running programs as well as threads that might even lead to denial-of-service (DoS) attacks
- RuntimeException, Exception, or Throwable should not be thrown or else it may lead to various errors
- printStackTrace() function should not be used in code as it reveals all the exception information to the user
- In Java, logging frameworks are provided from package java.util.logging and used to write log messages on to a central location