首页 > 代码库 > MyBatis应用开发(7)日志之自定义日志实现
MyBatis应用开发(7)日志之自定义日志实现
1.1. 自定义日志实现
实现MyBatis提供的org.apache.ibatis.logging.Log接口即可。
public interface Log { boolean isDebugEnabled(); boolean isTraceEnabled(); void error(String s, Throwable e); void error(String s); void debug(String s); void trace(String s); void warn(String s); }
一个最简单的日志实现如下所示:
/** * @Title: CustomLog.java * @Package com.test.mybatis3.log * @Description: * @author http://www.cnblogs.com/coe2coe/ * @date 2017年4月11日 下午9:33:12 * @version V1.0 */ package com.test.mybatis3.log; import org.apache.ibatis.logging.Log; /** * @ClassName: CustomLog * @Description: * @author http://www.cnblogs.com/coe2coe/ * @date 2017年4月11日 下午9:33:12 * */ public class CustomLog implements Log { public CustomLog(String clazz){ //将会输出CustomLog:org.apache.ibatis.logging.LogFactory。 System.out.println("CustomLog:" + clazz); } /* 是否输出DEBUG信息 * @see org.apache.ibatis.logging.Log#isDebugEnabled() */ @Override public boolean isDebugEnabled() { return true; } /* 是否输出TRACE信息。 * @see org.apache.ibatis.logging.Log#isTraceEnabled() */ @Override public boolean isTraceEnabled() { return true; } /* 输出ERROR信息。 * @see org.apache.ibatis.logging.Log#error(java.lang.String, java.lang.Throwable) */ @Override public void error(String s, Throwable e) { System.out.println("ERROR:"+s); e.printStackTrace(); } /* 输出ERROR信息。 * @see org.apache.ibatis.logging.Log#error(java.lang.String) */ @Override public void error(String s) { System.out.println("ERROR:"+s); } /* 输出DEBUG信息 * @see org.apache.ibatis.logging.Log#debug(java.lang.String) */ @Override public void debug(String s) { System.out.println("DEBUG:"+s); } /* 输出TRACE信息。 * @see org.apache.ibatis.logging.Log#trace(java.lang.String) */ @Override public void trace(String s) { System.out.println("TRACE:"+s); } /* 输出WARN信息。 * @see org.apache.ibatis.logging.Log#warn(java.lang.String) */ @Override public void warn(String s) { System.out.println("WARN:"+s); } }
此时在SqlMapConfig.xml文件中配置如下:
<!-- 使用com.test.mybatis3.log.CustomLog日志 --> <setting name="logImpl" value="com.test.mybatis3.log.CustomLog" />
本文介绍内容基于前面介绍的基于XML方式的例子程序和XML配置,除增加了logImpl配置之外。
运行结果如下:
by Mapper
CustomLog:org.apache.ibatis.logging.LogFactory
DEBUG:Logging initialized using ‘class com.test.mybatis3.log.CustomLog‘ adapter.
CustomLog:org.apache.ibatis.datasource.pooled.PooledDataSource
DEBUG:PooledDataSource forcefully closed/removed all connections.
DEBUG:PooledDataSource forcefully closed/removed all connections.
DEBUG:PooledDataSource forcefully closed/removed all connections.
DEBUG:PooledDataSource forcefully closed/removed all connections.
CustomLog:com.test.mybatis3.mapper.PersonMapper.findAllPersons
CustomLog:org.apache.ibatis.transaction.jdbc.JdbcTransaction
CustomLog:org.apache.ibatis.executor.BaseExecutor
DEBUG:Opening JDBC Connection
DEBUG:Created connection 4015549.
DEBUG:Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@3d45bd]
DEBUG:==> Preparing: select * from t_person order by id asc
DEBUG:==> Parameters:
TRACE:<== Columns: id, name, status
TRACE:<== Row: lisi, li si, 0
TRACE:<== Row: zhangsan, zhang san, 0
DEBUG:<== Total: 2
Person [id=lisi, name=li si, status=0]
Person [id=zhangsan, name=zhang san, status=0]
DEBUG:Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@3d45bd]
DEBUG:Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@3d45bd]
DEBUG:Returned connection 4015549 to pool.
MyBatis应用开发(7)日志之自定义日志实现