Java Log Performance optimization

Source: Internet
Author: User
Tags log4j

1. Select a reasonable log level and control the log content reasonably

2. Control the output content and format of the log

Logger.debug ("Entry number:" + i + "is" +  string.valueof (Entry[i])) and Logger.debug ("Entry number: {} is {}", I, entry[i]);

The above two statements have the same effect on the log output, but the overhead is not the same, the main effect is on string conversion and string concatenation, which converts the variables into strings and splices, regardless of whether they are in effect, and the latter only performs these operations when needed. LOG4J's official Test concludes that the performance of the two can vary by two orders of magnitude. Imagine how much resources can be saved if the ToString () method of an object uses Tostringbuilder to reflect the output of dozens of properties.


Therefore, some companies that are still using log4j 1.x or Apache Commons Logging (which do not support the notation of the {} template) will have the appropriate coding specifications that require additional judgment before a certain level of logging (such as debug and info) output:

if (logger.isdebugenabled ()) {    logger.debug ("Entry number:" + i + "is" + string.valueof (Entry[i]));} 

In addition to the log level and log messages, usually in the log also contains some other information, such as date, thread name, class information, MDC variables and so on, according to TAKIPI test, if you add class in the log, performance will drop sharply, compared to the default configuration of Logback, the throughput of the decline of about 60%. If you must print class information, consider naming logger with the class name.

In a distributed system, a request may go through several different subsystems, it is better to generate a UUID attached to the request, each subsystem in the print log when the UUID is placed in the MDC, to facilitate subsequent query related logs.

Http://www.infoq.com/cn/articles/things-of-java-log-performance

Java Log Performance optimization

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.