MyBatis log

MyBatis log factory provides a built-in logging, the logging facility to log the following Agents:
1.SLF4J
2.Apache Commons Logging
3.Log4j 2
4.Log4j
5.JDK logging
introspection mechanism to run its built-in log-based facility to choose the right logging tool, use the find tool to get the first one, if not found, the log functionality will be disabled. Classpath many applications server (such as Tomcat and WebShpere) already contains a Commons logging, so in this configuration environment logging tool as it MyBatis, MyBatis Log4J configuration will be ignored.
The first solution to this situation is to add a setting in which to select mybatis-config.xml other logging tools, configuration is as follows:

<configuration>
  <settings>
    ...
    <setting name="logImpl" value="LOG4J"/>
    ...
  </settings>
</configuration>

logImpl optional values ​​are: SLF4J, LOG4J, LOG4J2, JDK_LOGGING, COMMONS_LOGGING, STDOUT_LOGGING, NO_LOGGING,

The second method:
implement an interface org.apache.ibatis.logging.Log, and the constructor is fully qualified class name as a parameter string

The third method:

org.apache.ibatis.logging.LogFactory.useSlf4jLogging();
org.apache.ibatis.logging.LogFactory.useLog4JLogging();
org.apache.ibatis.logging.LogFactory.useJdkLogging();
org.apache.ibatis.logging.LogFactory.useCommonsLogging();
org.apache.ibatis.logging.LogFactory.useStdOutLogging();

If the above method is called it to be called before calling other methods MyBatis addition, the logging tool path is present only when the operation class, the call corresponding to the above process takes effect. Otherwise, MyBatis not and will ignored, instead of the default search order to find the log tool.

Log configuration:
Step one: Add the Log4J jar package
Step Two: Configure Log4J, create a file named log4j.properties in the classpath files:

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

The detailed configuration performs recording operations corresponding mapper and other types of recording only an error message applications.
1. Fine-grained configuration on the particular statement is as follows:

log4j.logger.org.mybatis.example.BlogMapper.selectBlog=TRACE

This configuration only selectBlog statement log
2. For a set of mapping interface logs:

log4j.logger.org.mybatis.example=TRACE

3. record only execute the SQL statement does not record the results configuration:

log4j.logger.org.mybatis.example=DEBUG

4. Record file without recording mapper mapping interface:

log4j.logger.org.mybatis.example.BlogMapper=TRACE

Guess you like

Origin blog.csdn.net/weixin_43638314/article/details/93380765