How logging in Java affects performance?

Does Logging in java affects the performance of the application?    
    Logging is an important component of the software development. A well-written logging code offers quick debugging, easy maintenance, and structured storage of an application’s runtime information.
    Logging does have its drawbacks also. It can slow down an application as it uses some CPU cycles and other resources (memory, etc). To answer these concerns, log4j is designed to be reliable, fast and extensible. Also, one should only log an error or something that must absolutely be logged. You can also log information helpful for debugging in the debug channel so it won’t affect production performance.
    Make sure you have a proper logging strategy. That means make sure you define what to log and have it separated into debug, trace, info, warning and error as well some others if you need them. Also make sure you give the ability to switch it on and off to enact performance/debugging as needed.
   Java logging severely affects performance of your application. Its quite common sense that more you log, more you perform file IO which slows down your application. That’s why choosing correct java logging level for every single message is quite important. Since having no java logging is not a choice you have to have logging in java application, what you can control is logging level and logging messages on that level. So always log DEBUG messages inside isDebugEnabled() block as shown in below example of debug mode in java.
logger.debug("java logging level is DEBUG Enabled");

To improve performance:

>>  Change the ConversionPattern to use less expensive date/time formatting, and (more importantly) avoid ‘C’, ‘F’, ‘L’ and ‘M’ because they are particularly expensive.
>>   Change the logging Threshold to INFO or WARNING or ERROR to reduce the amount of logging,

>>   Put the Logger.debug(…) call inside an if statement that checks that debug logging is enabled. This   saves the cost of assembling the log message in cases where it won’t be needed;

>>  You can restrict junk logs like this. Set the root logger as INFO so that unnecessary debug logs won’t come and fill up your log file.
log4j.rootCategory=INFO, A1

>>   If you want specific class or package to give out DEBUG logs you can do it like this.,A1

The above will print DEBUG level logs from the class DefaultLoadEventListener in your log file along with other INFO level logs.

Leave Comment

Your email address will not be published. Required fields are marked *

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

Get the Framework code at Github Repo: