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