Log specification (code out efficiency: detailed explanation of Alibaba Java development manual)
1. [mandatory] the API in the log system (Log4j, Logback) cannot be directly used in the application, but the API in the log framework SLF4J should be used. The log framework in facade mode is used, which is conducive to the unification of maintenance and log processing methods of various types.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static final Logger logger = LoggerFactory.getLogger(Abc.class);
2. [mandatory] it is recommended to keep the log file for at least 15 days, because some abnormalities occur frequently in "week".
3. [mandatory] the naming method of extended logs (such as management, temporary monitoring, access logs, etc.) in the application: appName_logType_logName.log. Logtype: log type. Recommended categories include stats/desc/monitor/visit, etc; Logname: log description. The benefits of this naming: you can know the application, type and purpose of the log file through the file name, which is also conducive to classification and search.
Positive example: mppserver application monitors time zone conversion exceptions separately, such as mppserver_monitor_timeZoneConvert.log Description: it is recommended to classify logs, such as storing error logs and business logs separately, which is convenient for developers to view and timely monitor the system through logs.
4. [mandatory] the log output of trace/debug/info level must be in the form of conditional output or placeholder. Description: logger debug("Processing trade with id: " + id + " symbol: " + symbol); If the log level is warn, the above log will not be printed, but the string splicing operation will be performed. If the symbol is an object, the toString() method will be executed, which wastes system resources. After the above operations, the final log will not be printed. Positive example: (condition)
if (logger.isDebugEnabled()) {
logger.debug("Processing trade with id: " + id + " symbol: " + symbol);
}
Positive example: (placeholder)
logger.debug("Processing trade with id: {} symbol : {} ", id, symbol);
5. [mandatory] avoid printing logs repeatedly and wasting disk space. Be sure to print logs in log4j Set additivity=false in XML. Positive example: < logger name = "com. Taobao. Dubbo. Config" additivity = "false" >
6. [mandatory] abnormal information should include two types of information: crime scene information and abnormal stack information. If not, throw it up through the keyword throw. Positive example: logger Error (various parameters or objects toString + ""+ e.getMessage(), e);
7. [recommended] record the log carefully. The production environment prohibits the output of debug logs; Selectively output info log; If you use warn to record the business behavior information when you first go online, you must pay attention to the problem of log output, avoid bursting the server disk, and remember to delete these observation logs in time. Note: outputting a large number of invalid logs is not conducive to the improvement of system performance and the rapid positioning of error points. When recording logs, please think: are these logs really read by anyone? What can you do when you see this log? Can it bring benefits to troubleshooting?
8. [reference] the warn log level can be used to record the error of user input parameters, so as to avoid being at a loss when users complain. Note the level of log output. The error level only records important error information such as system logic error and exception. If not necessary, please do not type the error level in this scenario.
Slf4j integrates many logging frameworks. You can use the logging framework by introducing slf4j API alone. If you introduce a specific jdk14, you will apply the specific logging framework through slf4j API. Jdk14 itself depends on slf4j API usage.
<modules> <module>slf4j-api</module> <module>slf4j-simple</module> <module>slf4j-nop</module> <module>slf4j-jdk14</module> <module>slf4j-log4j12</module> <module>slf4j-jcl</module> <module>slf4j-android</module> <module>slf4j-ext</module> <module>jcl-over-slf4j</module> <module>log4j-over-slf4j</module> <module>jul-to-slf4j</module> <module>osgi-over-slf4j</module> <module>integration</module> <module>slf4j-site</module> <module>slf4j-migrator</module> </modules>
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.26</version> </dependency> public class Slf4jLogDemo { private static final Logger LOGGER = LoggerFactory.getLogger(Slf4jLogDemo.class); public static void main(String[] args) { LOGGER.info("This is info log information,Print information before code"); System.out.println("output print information"); LOGGER.error("This is info log information,Print information after code"); } }
Integrate JDK14
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.26</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> <version>1.7.26</version> </dependency>
Integrate simple
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.26</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.26</version> </dependency>
Integrate Log4j
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.26</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.26</version> </dependency>
Integrate Logback