The log function is usually used in projects. You can find a common one. during the last two weeks, I encountered a problem at the user's site. I found that I had to pay great attention to the two items on the project and product: log + exception handling. First of all, the log shows that the problems at the user's site are generally not easy to reproduce. If they are easy to reproduce, it indicates that the initial test was not successful. Logs are the only real source of information. The feedback from customers and field support personnel may be misleading in some cases. If the logs are not powerful at this time, then you will know what it is. This time, some of the log output on the site encountered a problem. Only the exception information was found, and the stack was not even typed. It was impossible to determine where the logs were generated, why is this exception caught there. You can do it. Exception Handling has always been the biggest headache for projects and products. What exceptions should be captured and how to handle them? If logs need to be output, what level of logs are output. These exceptions must be thrown, and when the throw action will be stopped. Let's talk about the on-site issue. Some exceptions in the project are normal, such as Socket interruption and I/O read errors. They are all normal exceptions. It is acceptable to capture and output FINE-level logs. However, some threads are too hard to capture, such as catch throwable, and even OOM, they cannot get out. What's more, the output log is actually at the FINE level. In the production environment, logs are not enabled at the FINE level. Of course, these logs are merged. Eventually, the system keeps making errors and the log file does not respond. To improve the log system in the next stage, we found that most open-source products, including Geronimo and mina, use SLF4J. I decided to take a deeper look. SLF4J (Simple Logging Facade for Java) is a common Log Facade library that provides a set of common Log interfaces for various Log implementations. SLF4J determines the used log implementation Library Based on the adapter jar in classpath. This completes the implementation framework of dynamic modification of system logs. For example, we often use log4j. If our project uses SLF4J + Log4j to build log output, we need three JAR files: 1. slf4j-api-1.6.4.jar2. slf4j-log4j12-1.6.4.jar3. log4j-1.2.13.jarslf4j-api-1.6.4.jar is Slf4j is Slf4j interface, is also our project compilation needs to rely on JAR files. The slf4j-log4j12-1.6.4.jar is the jar of the adapter mentioned above. Log4j-1.2.13.jar Needless to say, the JAR file of log4j. In fact, the slf4j-api-1.6.4.jar is very simple, I believe you can read his code in a few hours or less, 20 classes just now seems to be 23 ). Then, how does SLF4J determine the used log implementation Library Based on the adapter jar in classpath. In fact, each adapter jar contains a special class: Org. slf4j. impl. StaticLoggerBinder. class. Implement the LoggerFactoryBinder interface: public interface LoggerFactoryBinder {
Public ILoggerFactory getLoggerFactory ();
Public String getLoggerFactoryClassStr ();
} He is a hungry Singleton class, that is, ILoggerFactory that obtains implementation logs (log4j here) through the getLoggerFactory method of this class, and then obtains the corresponding Logger of log4j through ILoggerFactory. the relationship between Slf4j and Log4j is completed by using multiple familiar names. SLF4J first searches for Org. slf4j. impl. StaticLoggerBinder. class in classpath. If it is found, the pair is successful. If more than one StaticLoggerBinder is found, the user will be prompted to keep only one Class path contains multiple SLF4J bindings ). Some log implementations that are not fully related to Slf4j implement their own adapters, such as currently popular logback. To configure logback logs, you only need two JAR: slf4j-api-1.6.4.jar, logback1.0.3.jar. This class is generally later than Slf4j development. At present, no log implementation has taken the initiative to make up for the adapter gap. Log4j, slf4j, and logback are all the same authors. Finally, we recommend several logback source code analysis articles. The http://www.iteye.com/topic/1114057www.iteye.com/topic/1114485http://www.iteye.com/topic/1114489http://www.iteye.com/topic/1114619 has summarized several issues about Exception Handling: 1. Non-handled exceptions are not captured. The day before yesterday, a colleague wrote: Try {// some op} catch (IOException e) {Throw e;}. This is a bad habit. Unless you want to throw an IOException, consider this if other exceptions are not thrown.
2. Only exceptions are caught in the method, and Throwable. In particular, VirualMachineError is not required.
3. Do not wrap the original Exception when there is no necessary information appended. unless an Exception is converted, for example, the checkException is converted to uncheckException4. exceptions are not captured in general, because RuntimeException may be eaten. 5. Exception logs are output once. An exception is output only in a suitable place. On the one hand, the code is concise, and on the other hand, it is convenient to locate the problem.
This article from the "World without thieves" blog, please be sure to keep this source http://guojuanjun.blog.51cto.com/277646/854962