Detailed explanation of log printing in combination with log4j and slf4j under SSM framework

This paper mainly introduces how to print logs in combination with log4j and slf4j under the detailed SSM framework and share them with you. The details are as follows:

First, add the jar packages of log4j and slf4j

The built-in log factory of mybatis provides log function. The specific log implementation tools are as follows:

1. The specific log implementation tool selected is determined by mybatis's built-in log factory. It uses the first found (in the order listed above). If none is found, the logging function is disabled.

Common logging is already included in the classpath of many application servers, such as Tomcat and WebShpere, so mybatis will implement it as a specific log. It's important to remember that. This will mean that in environments such as WebSphere, which provides a private implementation of common logging, your log4j configuration will be ignored. This practice is sad. How can mybatis ignore your configuration? In fact, since common logging already exists, log4j is naturally ignored according to the priority order! However, if your application is deployed in an environment containing common logging and you want to use other logging frameworks, you can use mybatis config. In the mybatis configuration file XML to select a different log implementation.

The optional values of logimpl are slf4j, log4j, log4j2 and JDK_ LOGGING、COMMONS_ LOGGING、STDOUT_ LOGGING、NO_ Logging or implementing the interface org apache. ibatis. logging. The fully qualified class name of the log class, and the constructor of this class needs to take a string (string type) as the parameter. (you can refer to the implementation of org.apache.ibatis.logging.slf4j.slf4jimpl.java)

You can call one of the following methods as needed:

If you really need to call a method above, please call it before all other MyBatis methods. In addition, it is meaningful to call the corresponding method only if the corresponding log implementation exists, otherwise mybatis will ignore it. If log4j does not exist in your environment, but you call the corresponding method, mybatis will ignore this call and replace the default search order to find the log implementation.

2. Configure web xml

3. Just create one named log4j. In the classpath of the application Properties. The specific contents of the file are as follows:

As shown in the figure, when we enter debug, SQL statements will be printed

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>