springboot項目mybatis日誌自定義設置無法生效,就是無法設置日誌級別,無法對java.sql.PreparedStatement
、java.sql.Connection
等進行設置。
翻了spring和mybatis官網以及幾十篇文章都沒找到一個好使的方法,大概是項目中有什麼奇怪的衝突的原因吧,不準備找了,直接自定義日誌搞起。
yml中的mybatis配置
mybatis:
mapper-locations: classpath:/mapper/*/*xml
configuration:
#log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
log-impl: testMavenWeb.log.CustomLog
jdbcTypeForNull: VARCHAR
CustomLog是實現org.apache.ibatis.logging.Log接口的自定義日誌類。
先來複習一下日誌類時這麼運行的,下面是CustomLog代碼
import org.apache.ibatis.logging.Log;
public class CustomLog implements Log {
public CustomLog(String clazz) {
// Do Nothing
}
@Override
public boolean isDebugEnabled() {
return true;
}
@Override
public boolean isTraceEnabled() {
return true;
}
@Override
public void error(String s, Throwable e) {
System.err.println(s);
e.printStackTrace(System.err);
}
@Override
public void error(String s) {
System.out.println("mylog error");
System.err.println(s);
}
@Override
public void debug(String s) {
System.out.println("mylog debug");
System.out.println(s);
}
@Override
public void trace(String s) {
System.out.println("mylog trace");
System.out.println(s);
}
@Override
public void warn(String s) {
System.out.println("mylog warn");
System.out.println(s);
}
}
sql代碼
<select id="testQueryList" resultType="java.util.HashMap">
<![CDATA[
select level || #{data} lv from dual connect by level <= 20
]]>
</select>
現在我後臺的日誌是這樣的
mylog debug
Creating a new SqlSession
mylog debug
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@26a8b664] was not registered for synchronization because synchronization is not active
mylog debug
JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@22972669] will not be managed by Spring
mylog debug
==> Preparing: select level || ? lv from dual connect by level <= 20
mylog debug
==> Parameters: ddd(String)
mylog trace
<== Columns: LV
mylog trace
<== Row: 1ddd
mylog trace
<== Row: 2ddd
mylog trace
<== Row: 3ddd
mylog trace
<== Row: 4ddd
mylog trace
<== Row: 5ddd
mylog trace
<== Row: 6ddd
mylog trace
<== Row: 7ddd
mylog trace
<== Row: 8ddd
mylog trace
<== Row: 9ddd
mylog trace
<== Row: 10ddd
mylog trace
<== Row: 11ddd
mylog trace
<== Row: 12ddd
mylog trace
<== Row: 13ddd
mylog trace
<== Row: 14ddd
mylog trace
<== Row: 15ddd
mylog trace
<== Row: 16ddd
mylog trace
<== Row: 17ddd
mylog trace
<== Row: 18ddd
mylog trace
<== Row: 19ddd
mylog trace
<== Row: 20ddd
mylog debug
<== Total: 20
mylog debug
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@26a8b664]
剩下的就簡單了,比如我如果想要屏蔽ResultSet
,則只需要屏蔽trace
中的代碼即可
@Override
public void trace(String s) {
//System.out.println("mylog trace");
//System.out.println(s);
}
現在這個日誌設置已經夠用,日後需要時再做完善吧。