标签归档:log

mybatis 日志配置

mybatis 版本:

<dependency>
    <groupid>org.mybatis</groupid>
    <artifactid>mybatis</artifactid>
    <version>3.4.6</version>
</dependency>

使用slf4j-api进行日志输出, 日子实现使用logback,需要依赖,

<dependency>
     <groupid>org.slf4j</groupid>
     <artifactid>slf4j-api</artifactid>
     <version>1.7.25</version>
</dependency>
<dependency>
    <groupid>ch.qos.logback</groupid>
    <artifactid>logback-classic</artifactid>
    <version>1.2.3</version>
</dependency>

在mybatis-config.xml 中增加如下配置,

<!--?xml version="1.0" encoding="UTF-8" ?-->
 
<configuration>
    <!-- 配置全局属性 -->
    <settings>
 
 
        <!-- log -->
        <setting name="logImpl" value="SLF4J">
 
    </setting></settings>
</configuration>

logback.xml中增加如下配置,

<appender name="mybatis" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <!--See also http://logback.qos.ch/manual/appenders.html#RollingFileAppender-->
    <append>true</append>
    <file>${log.root}/logs/mybatis/mybatis.log</file>
    <encoder>
        <pattern>%d{"yyyy-MM-dd HH:mm:ss,SSS"} %-5level [%thread] %class{256}.%M %L - %msg%n</pattern>
    </encoder>
 
    <rollingpolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <maxindex>3</maxindex>
        <filenamepattern>${log.root}/logs/mybatis/mybatis.log.%i</filenamepattern>
    </rollingpolicy>
    <triggeringpolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <maxfilesize>1024KB</maxfilesize>
    </triggeringpolicy>
</appender>
 
 
<logger name="org.mybatis" level="DEBUG" additivity="false">
    <appender-ref ref="mybatis">
</appender-ref></logger>
 
<logger name="java.sql" level="DEBUG" additivity="false">
    <appender-ref ref="mybatis">
</appender-ref></logger>
 
<logger name="YOUR_DAO_PACKAGE" level="DEBUG" additivity="false">
    <appender-ref ref="mybatis">
</appender-ref></logger>

其中 YOUR_DAO_PACKAGE是你的dao的包。

经过上述配置,你的mybatis将可以输出sql等关键信息到日志文件。