springboot项目mybatis日志自定义设置无法生效

springboot项目mybatis日志自定义设置无法生效,就是无法设置日志级别,无法对java.sql.PreparedStatementjava.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);
	}

现在这个日志设置已经够用,日后需要时再做完善吧。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章