log convention
1. [Mandatory] The application cannot directly use the API in the log system (Log 4 j, Logback), but should rely on the API in the log framework SLF 4 J, and use the facade mode log framework. It is conducive to maintenance and unification of log processing methods for each category. import org.slf4j.Logger;
import org.slf4j.LoggerFactory; private static final Logger logger = LoggerFactory.getLogger(Abc.class);
2. [Mandatory] It is recommended to save log files for at least 15 days, because some exceptions have the characteristic of occurring with a "weekly" frequency.
3. [Mandatory] Extended logs in applications (such as management, temporary monitoring, access logs, etc.) Naming method:
appName _ logType _ logName .log. logType: log type, recommended categories include
stats / desc / monitor / visit, etc.; logName: log description. The advantage of this kind of naming: through the file name, you can know what application the log file belongs to, what type, and what purpose, which is also helpful for classification and search.
Positive example: Monitor the time zone conversion exception separately in the mppserver application, such as: mppserver _ monitor _ timeZoneConvert . log
Description: It is recommended to classify logs. Error logs and business logs should be stored separately as much as possible to facilitate developers' viewing and to monitor the system in a timely manner through logs.
Description:
logger . debug( " Processing trade with id : " id " symbol : " symbol);If the log level is warn, the above log It will not print, but the string splicing operation will be performed. If symbol is an object,
will execute the toString() method, which wastes system resources. After performing the above operation, the final log is not printed.Positive example:
(Condition) if (logger.isDebugEnabled()) {
logger.debug("Processing trade with id: " + id + " symbol: " + symbol);
}
(Placeholder) logger.debug("Processing trade with id: {} symbol : {} ", id, symbol);
5 . [Mandatory] To avoid printing logs repeatedly and wasting disk space, be sure to set additivity = false in log 4 j . xml.
<logger name="com.taobao.dubbo.config" additivity="false">
# #6. [Mandatory] Exception information should include two types of information: crime scene information and exception stack information. If it is not processed, then throw it up
.
Positive example:
logger.error(various parameters or objects toString "_" e.getMessage(), e);7. [Recommendation] You can use the warn log level to record user input parameter errors to avoid being at a loss when users complain . Pay attention to the level of log output. The error level only records important error information such as system logic errors and exceptions. If it is not absolutely necessary, please do not enter the error level in this scenario.
8. [Recommended] Record logs carefully. It is prohibited to output debug logs in the production environment; selectively output info logs; if you use
Explode and remember to delete these observation logs in time.
Note:Outputting a large number of invalid logs is not conducive to improving system performance or quickly locating error points. When recording logs, please think: Is anyone really reading these logs? What can you do after seeing this log? Can it bring benefits to troubleshooting?