mybatis日志

MyBatis 通过使用内部的日志工厂来提供日志信息,日志工厂可以授权下面这些日志实现类中的任何一种:
(1)SLF4J
(2)Apache Commons Logging
(3)Log4j 2
(4)Log4j
(5)JDK logging
MyBatis 内部的日志工厂解决方案选择时基于运行的introspection ,MyBatis 日志工厂将使用它所发现的第一次使用日志的实现(在上面的顺序中搜索实现)。如果MyBatis 没有发现上面的任何实现,日志将会被禁用。
很多环境把Commons Logging作为应用服务路径的一部分(比如WebSphere和Tomcat ),在这样的环境中知道这些是很重要的,MyBatis 将会使用Commons Logging作为日志的实现。在像WebSphere 这样的环境中,意味着你的Log4J 配置将会被忽略,因为WebSphere 提供他自己的 Commons Logging专有的实现。因为它的出现会让MyBatis 忽略你的Log4J 配置(实际,MyBatis 忽略你的Log4J 配置是因为MyBatis 再这样的环境中将会使用Commons Logging),这是很令人沮丧的。如果你的应用正在一个classpath中包含Commons Logging的环境中运行,但是你却想使用其他的日志实现,此时你可以通过在mybatis-config.xml 文件中添加如下的设置来选择一个不同的日志实现,代码如下:

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

有效的value包括SLF4J, LOG4J, LOG4J2, JDK_LOGGING, COMMONS_LOGGING, STDOUT_LOGGING, NO_LOGGING 或者一个全限定实现了org.apache.ibatis.logging.Log ,并且有个拥有String类型参数的构造器的类。
你也可以选择调用下面这些方法的实现:

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();

如果你选择调用这些方法之一,你应该在调用MyBatis的任何方法之前这样做。而且,这些方法只可以转换到请求日志实现,当然这些实现应该可以在运行时路径上被执行。例如,如果你尝试选择Log4J 日志,Log4J 在运行时不可以被执行,接着MyBatis 就会忽略请求使用Log4J ,并且会使用正常的运算法则来发现日志的实现。
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:
SLF4J, Apache Commons Logging, Apache Log4J and the JDK Logging API 的细节超出了这篇文档的范围。然而,下面的这个配置例子你应该明白。如果你想了解更多这些框架,你可以从下面的这些中获取更多的信息。

  • http://www.slf4j.org/
  • http://commons.apache.org/proper/commons-logging/
  • http://logging.apache.org/log4j/2.x/
  • http://www.oracle.com/technetwork/java/index.html
    日志配置
    为了看到MyBatis 日志语句你可以打开一个包,完全映射的类名,一个命名空间或者一个全限定的语句名字。
    你怎样做取决于你在使用时日志的实现。我们将以Log4J作为示范来演示。配置日志服务是一个简单的事情,只需要包含一个或者更多的额外配置文件(比如:log4j.properties)或者有时候需要添加一个新jar(比如:log4j.jar)。下面的配置例子将会通过使用Log4J 来配置所有的日志服务。这里有两个步骤:
    步骤一:添加Log4J JAR文件
    因为我们使用的是Log4J,我们将需要确定它的JAR文件对于我们的应用程序是可用的。为了使用Log4J,你需要添加JAR文件到你的应用路径中。你可以通过上面的URL地址来下载Log4J。
    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.
    对于web或者企业级应用你可以在你的 WEB-INF/lib 目录中添加 log4j.jar
    步骤2:配置log4j
    Configuring Log4J is simple. Suppose you want to enable the log for this mapper:
    配置Log4J 很简单,假设你想打开这个映射的日志:
package org.mybatis.example;
public interface BlogMapper {
  @Select("SELECT * FROM blog WHERE id = #{id}")
  Blog selectBlog(int id);
}

创建一个叫log4j.properties的文件夹,正如下面所显示的,把它放到你的路径中:

# 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

上面的文件将会使得log4J 来报告org.mybatis.example.BlogMapper的详细的日志信息,和你的应用剩余类的错误。
如果你想在一个较好的级别调整日志,你可以用具体的语句来打开日志,而不是打开所有的映射文件,下面的这行代码可以打开selectBlog声明的日志:

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

相反,或许你想打开一组映射的日志,在那种情况下,你应该在属于你的映射中增加一个logger

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.
这有 可以返回巨大的结果集的问题,在那种情况下,你或许想看SQL语句而不是结果集,为了那个目的,SQL语句被记录在调试级别(在JDK日志中 (FINE in JDK logging) ),结果集处于追踪级别(FINER in JDK logging),所以如果你想要查看语句而不查看结果,请将级别设置为调试.。

log4j.logger.org.mybatis.example=DEBUG

但是如果你不使用映射接口而使用映射的XML文件会怎么样?

<?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:
在那种情况下你可以通过为namespace添加一个logger来打开所有的日志XML文件,正如下面的代码:

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

或者对于一个明确的语句:

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.
是的,正如你所注意到的,对于在配置日志的时候,使用映射接口或者是XML配置文件是没有区别的。
注意:如果你正在MyBatis 中使用SLF4J 或者Log4j 2,将会称之为使用标记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.
在log4j.properties文件剩余的配置是被用来配置附加项,这些附加项超出了本文档的作用范围。然而,你可以发现更多的信息在Log4J的网页中(就是前面的URL链接)。或者,你可以用它来简化实验来看看不同配置选项有什么影响。

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值