1 MyBatis 日志
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:
MyBatis 通过使用内置的日志工厂提供日志信息。内置的日志工厂会将日志信息委托给以下其中一种日志工具:
- 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.
MyBatis 内置日志工厂基于运行时自检来选择日志工具。MyBatis 日志工厂将使用它发现的第一个日志工具(按上面列举的顺序查找工具)。如果 MyBatis 一个都没有找到,那么日志功能就会被禁用。
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:
很多环境把 Commons Logging 作为应用服务器类路径的一部分(比如 Tomcat 和 WebSphere)。有很重要的一点要知道,在这样的环境中,MyBatis 将使用 Commons Logging 作为日志工具。这就意味着,在像 WebSphere 这样的环境中,你的 Log4J 配置将会被忽略掉,因为 WebSphere 提供了 Commons Logging 的私有实现。对于MyBatis 忽略你的 Log4J 配置这一点是非常烦人的(事实上,MyBatis 忽略你的 Log4J 配置是因为在这样的环境中它将使用 Commons Logging)。如果你的应用运行的是一个类路径中包含 Commons Logging 的环境但你又想使用其他的日志工具,你可以通过在 mybatis-config.xml 文件中添加一行设置来选择一个不同的日志工具:
<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.
有效的值有:SLF4J, LOG4J, LOG4J2, JDK_LOGGING, COMMONS_LOGGING, STDOUT_LOGGING, NO_LOGGING 或一个实现了 org.apache.ibatis.logging.Log 并有一个带 String 参数的构造方法的类的完全限定类名。
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.
如果你选择调用其中一个方法,你应该在调用其他 MyBatis 方法之前调用它。而且,仅在运行期类路径中存在该日志工具时,这些方法才会转换请求的日志工具。例如,如果你试着选择 Log4J 日志但是 Log4J 在运行期不可用,那么 MyBatis 将会忽略使用 Log4J 的请求,并且它将使用默认算法来查找日志工具。
通过下面链接获取更多信息:
2 日志配置
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.
要查看 MyBatis 的日志语句,你需要对包、映射器的完全限定类名、命名空间、完全限定语句名称开启日志功能。
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).
再次强调,如何做取决于使用的日志工具。我们将展示如何使用 Log4J 来做。配置日志服务很简单:添加一个或多个配置文件(如 log4j.properties),有时候需要添加一个新的 JAR 文件(如 log4j.jar)。
Step 1: 添加一个 Log4J JAR 包
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.
因为我们要使用 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 应用或企业级应用而言,你可以添加 log4j.jar 到你的 WEB-INF/lib 目录;对于独立应用你可以简单地将它添加到 JVM -classpath 的启动参数即可。
Step 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);
}
Create a file called log4j.properties as shown below and place it in your classpath:
创建一个名为 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
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.
上面的文件将使 log4J 为 org.mybatis.example.BlogMapper 报告日志详细信息且仅报告应用中其他类的错误信息。
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:
如果你想调整日志到一个更好的级别你可以为特定的语句打开日志功能而不是整个的映射器文件。下面这行配置将只为 selectBlog 开启日志:
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.
某些查询可能返回大量的结果集。这种情况下你可能只想要查看 SQL 语句而不是结果。为了实现这个目的,可以将 SQL 语句的日志级别设置为 DEBUG(JDK 的日志设置为 FINE),结果的日志级别设置为 TRACE(JDK 日志设置为 FINER)。因此如果你想要查看语句而不查看结果,设置日志级别为 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 映射文件而不是映射器接口怎么办?
<?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:
这种情况下你可以通过为命名空间添加一个 logger 来开启整个 XML 文件的日志功能:
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.
是的,就像你已经注意到的,为映射器接口和 XML 映射文件配置日志没什么不同。
NOTE If you are using SLF4J or Log4j 2 MyBatis will call it using the marker MYBATIS.
注意:如果你使用的是 SLF4J 或 Log4j 2,MyBatis 将以 MYBATIS 这个值来调用。
The remaining configuration in the log4j.properties file is used to configure the appenders. See Log4J website (URL above) for more information.
log4j.properties 文件中剩余的配置是用来配置日志输出源的。请查看 Log4J 官网获取更多信息。
最后
说明:MyBatis 官网提供了简体中文的翻译,但个人觉得较为生硬,甚至有些地方逻辑不通,于是自己一个个重新敲着翻译的(都不知道哪里来的自信...),有些地方同官网翻译有出入,有些倔强地保留了自己的,有的实在别扭则保留了官网的,这些都会在实践中一一更正。鉴于个人英文能力有限,文章中保留了官方文档原英文介绍(个别地方加以调整修剪),希望有缘看到这里的朋友们能够有自己的理解,不会被我可能错误或不合理的翻译带跑偏(〃'▽'〃),欢迎指正!
当前版本:mybatis-3.5.0
官网文档:MyBatis
官网翻译:MyBatis 简体中文
项目实践:MyBatis Learn
网友评论