`

MyBatis3日志配置(转官网)

 
阅读更多

来源:
http://mybatis.github.io/mybatis-3/logging.html

=====================================================

 

 

Logging

 

MyBatis provides logging information through the use of an internal log factory. The internal log factory will delegate logging information to one of the following log implementations:

  • SLF4J
  • Apache Commons Logging
  • Log4j 2
  • Log4j
  • JDK logging

The logging solution chosen is based on runtime introspection by the internal MyBatis log factory. The MyBatis log factory will use the first logging implementation it finds (implementations are searched in the above order). If MyBatis finds none of the above implementations, then logging will be disabled.

Many environments ship Commons Logging as a part of the application server classpath (good examples include Tomcat and WebSphere). It is important to know that in such environments, MyBatis will use Commons Logging as the logging implementation. In an environment like WebSphere this will mean that your Log4J configuration will be ignored because WebSphere supplies its own proprietary implementation of Commons Logging. This can be very frustrating because it will appear that MyBatis is ignoring your Log4J configuration (in fact, MyBatis is ignoring your Log4J configuration because MyBatis will use Commons Logging in such environments). If your application is running in an environment where Commons Logging is included in the classpath but you would rather use one of the other logging implementations you can select a different logging implementation by adding a setting in mybatis-config.xml file as follows:

<configuration>
  <settings>
    ...
    <setting name="logImpl" value="LOG4J"/>
    ...
  </settings>
</configuration>
      

Valid values are SLF4J, LOG4J, LOG4J2, JDK_LOGGING, COMMONS_LOGGING, STDOUT_LOGGING, NO_LOGGING or a full qualified class name that implements org.apache.ibatis.logging.Log and gets an string as a constructor parameter.

You can also select the implementation by calling one of the following methods:

org.apache.ibatis.logging.LogFactory.useSlf4jLogging();
org.apache.ibatis.logging.LogFactory.useLog4JLogging();
org.apache.ibatis.logging.LogFactory.useLog4J2Logging();
org.apache.ibatis.logging.LogFactory.useJdkLogging();
org.apache.ibatis.logging.LogFactory.useCommonsLogging();
org.apache.ibatis.logging.LogFactory.useStdOutLogging();

If you choose to call one of these methods, you should do so before calling any other MyBatis method. Also, these methods will only switch to the requested log implementation if that implementation is available on the runtime classpath. For example, if you try to select Log4J logging and Log4J is not available at runtime, then MyBatis will ignore the request to use Log4J and will use it's normal algorithm for discovering logging implementations.

The specifics of SLF4J, Apache Commons Logging, Apache Log4J and the JDK Logging API are beyond the scope of this document. However the example configuration below should get you started. If you would like to know more about these frameworks, you can get more information from the following locations:

Logging Configuration

To see MyBatis logging statements you may enable logging on a package, a mapper fully qualified class name, a namespace o a fully qualified statement name.

Again, how you do this is dependent on the logging implementation in use. We'll show how to do it with Log4J. Configuring the logging services is simply a matter of including one or more extra configuration files (e.g. log4j.properties) and sometimes a new JAR file (e.g. log4j.jar). The following example configuration will configure full logging services using Log4J as a provider. There are 2 steps.

 

Step 1: Add the Log4J JAR file

Because we are using Log4J, we will need to ensure its JAR file is available to our application. To use Log4J, you need to add the JAR file to your application classpath. You can download Log4J from the URL above.

For web or enterprise applications you can add the log4j.jar to your WEB-INF/lib directory, or for a standalone application you can simply add it to the JVM -classpath startup parameter.

 

Step 2: Configure Log4J

Configuring Log4J is simple. Suppose you want to enable the log for this mapper:

package org.mybatis.example;
public interface BlogMapper {
  @Select("SELECT * FROM blog WHERE id = #{id}")
  Blog selectBlog(int id);
}

Create a file called log4j.properties as shown below and place it in your classpath:

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

The above file will cause log4J to report detailed logging for org.mybatis.example.BlogMapper and just errors for the rest of the classes of your application.

If you want to tune the logging at a finer level you can turn logging on for specific statements instead of the whole mapper file. The following line will enable logging just for the selectBlog statement:

log4j.logger.org.mybatis.example.BlogMapper.selectBlog=TRACE

By the contrary you may want want to enable logging for a group of mappers. In that case you should add as a logger the root package where your mappers reside:

log4j.logger.org.mybatis.example=TRACE

There are queries that can return huge result sets. In that cases you may want to see the SQL statement but not the results. For that purpose SQL statements are logged at the DEBUG level (FINE in JDK logging) and results at the TRACE level (FINER in JDK logging), so in case you want to see the statement but not the result, set the level to DEBUG.

log4j.logger.org.mybatis.example=DEBUG

But what about if you are not using mapper interfaces but mapper XML files like this one?

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.example.BlogMapper">
  <select id="selectBlog" resultType="Blog">
    select * from Blog where id = #{id}
  </select>
</mapper>

In that case you can enable logging for the whole XML file by adding a logger for the namespace as shown below:

log4j.logger.org.mybatis.example.BlogMapper=TRACE

Or for an specific statement:

log4j.logger.org.mybatis.example.BlogMapper.selectBlog=TRACE

Yes, as you may have noticed, there is no difference in configuring logging for mapper interfaces or for XML mapper files.

NOTE If you are using SLF4J or Log4j 2 MyBatis will call it using the marker MYBATIS.

The remaining configuration in the log4j.properties file is used to configure the appenders, which is beyond the scope of this document. However, you can find more information at the Log4J website (URL above). Or, you could simply experiment with it to see what effects the different configuration options have.

分享到:
评论

相关推荐

    MyBatis 3 开发指南(中文版)

    MyBatis起源于iBatis,iBatis是Clinton Begin在2002年发起的开源项目,于2010年6月16日从Apache的网站退役,并被Google Code托管,改名为MyBatis。MyBatis是一个支持普通SQL查询、存储过程和高级映射的优秀的持久层...

    tidb(mysql5.7) springboot mybatis-plus

    java Springboot开发必备环境 : ...统一参数校验,自定义异常提醒,统一日志,统一响应返回,统一异常处理 。 推荐2: mybatis-plus 采用最新的生成代码工具 推荐3: 将多个基础功能整理后,并用单元测试验证。

    spring+springmvc+mybatis的整合

    1.建立项目,添加spring5\springMVC5\Mybatis3的maven 2.写spring、springmvc、mybatis的配置文件 2.1 spring配置文件,约束,哪里来? 源码中可以找sxd约束文件,这个的话我在 E:\2017下学期\spring-framework-...

    MyBatis Plus配置日志CRUD的使用详解

    主要介绍了MyBatis Plus配置日志,CRUD的使用,,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

    JavaEE企业级分布式高级架构师018期 94G

    ├─第1章 mybatis从入门到精通 │ │ 第一章第1节: 02-mybatis介绍.mp4 │ │ 第一章第1节: 03-mybatis入门.mp4 │ │ 第一章第1节: 04-mybatis基础应用...│ ├─第3节 9月5日 Mybatis02-手写Mybatis框架

    基于SpringMVC的教务查询系统(源码+项目操作说明+数据库).zip

    教务查询系统,熟悉SSM的整合开发【使用技术】IOC容器:SpringWeb框架:SpringMVCORM框架:Mybatis安全框架:Shiro数据源:C3P0日志:log4j前端框架:Bootstrap【环境配置】编译器:IntelliJ IDEA项目构建工具:...

    基于jbpm与activiti的工作流平台技术架构介绍

    6.MyBatis 3 7.CKEditor 3.6 8.JQuery 1.8 9.CXF 2.0 10.Alfresco Activiti 5.8 11.Compass + Lucene 12.JasperReport 4.5 13.JavaMail 14.Other: Log4j,Velocity,Sitemesh,Jfreechart,Jforum,Solr 15.Maven 2 ...

    Activiti工作流学习资料.zip

    基于JDBC参数配置的数据库连接 会使用默认的MyBatis连接池。 下面的参数可以用来配置连接池(来自MyBatis参数): jdbcMaxActiveConnections: 连接池中处于被使用状态的连接的最大值。默认为10。 ...

    WiFiProbeAnalysis:基于WIFI探针的商业大数据分析技术

    2019年10月3日更新 系统核心, 负责实时计算以及离线计算 主要技术或API: 名称 解释 Spark 分析程序核心API Hadoop 分析程序核心API Mybatis 操作Mysql的API SharedJedis 分布式Redis的API Log4j 日志记录工具 ...

    最新基于SSM框架班级同学录网站.zip

    基于SSM框架的班级同学录网站是一个为高校或其他教育机构设计的在线社交平台,旨在帮助同班同学保持联系和信息共享。该平台利用Spring、SpringMVC和MyBatis(SSM)集成框架开发,提供了一套系统的解决方案用于班级...

    最新基于SSM框架艺诚美业管理系统.zip

    3. **员工管理**:管理员工的基本信息、排班、业绩统计等。 4. **服务项目管理**:维护服务项目列表,包括项目描述、价格、所需材料等。 5. **库存管理**:跟踪产品和材料的出入库情况,及时补货避免缺货。 6. **...

    最新基于SSM框架智能停车场管理系统.zip

    3. **自助缴费**:支持无感支付或自助缴费机缴费,提供多种支付方式如微信、支付宝等。 4. **会员管理**:为常客提供会员注册服务,包括积分累计、优惠活动等。 5. **报表统计**:生成日/周/月报,反映停车场使用...

    最新基于SSM框架医院打卡挂号系统.zip

    3. **自动排号**:系统能够根据挂号信息自动分配排队号码,优化候诊流程。 4. **签到确认**:患者在到达医院后可以在自助签到机上刷卡或扫描二维码进行签到,系统自动更新排队状态。 5. **医生端管理**:医生可以...

    wftest:Williams Forrest的代码测试

    wftest Williams Forrest的代码测试乔·韦德(Joe Weder)2015年9月27日该应用程序分为3个模块: 模型-包含域模型。 服务-DAO和服务层。 使用myBatis映射。 使用HSQLDB作为嵌入式数据库进行单元测试。 WebApp-Web...

    Guns-Separation-其他

    4、后端采用spring boot + mybatis-plus + hutool等,开源可靠。5、基于spring security(jwt) + 用户UUID双重认证。6、基于AOP实现的接口粒度的鉴权,最细粒度过滤权限资源。7、基于hibernate validator实现的校验...

    Guns-Separation v1.1

    4、后端采用spring boot + mybatis-plus + hutool等,开源可靠。 5、基于spring security(jwt) + 用户UUID双重认证。 6、基于AOP实现的接口粒度的鉴权,最细粒度过滤权限资源。 7、基于hibernate validator实现的...

Global site tag (gtag.js) - Google Analytics