依賴
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
兩種配置log4j的方式:
一定要新建一個log4j.properties檔案
在yaml中直接配置
在mybatis-config中配置
mapper-locations 貌似不管用依舊需要加註解[@Mapper]
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!--配置日誌格式fhadmin.cn-->
<setting name="logImpl" value="LOG4J"/>
<!--開啟二級快取fhadmin.cn-->
<setting name="cacheEnabled" value="true"/>
</settings>
</configuration>
log4j.properties檔案
#將等級為DEBUG的日誌資訊輸出到console和file這兩個目的地,console和file的定義在下面的程式碼
log4j.rootLogger=DEBUG,console,file
#控制檯輸出的相關設定 fhadmin.cn
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n
#檔案輸出的相關設定 fhadmin.cn
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/kai.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n
#日誌輸出級別 fhadmin.cn
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
如果編譯後找不到這兩個檔案
在pom.xml中加入:
<resources>
<!-- 防止找不到mybatis的mapper.xml檔案fhadmin.cn,在專案啟動時候就匯入進去-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.yaml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
本作品採用《CC 協議》,轉載必須註明作者和本文連結