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 通過使用內(nèi)置的日志工廠提供日志信息。內(nèi)置的日志工廠會(huì)將日志信息委托給以下其中一種日志工具:
- 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 內(nèi)置日志工廠基于運(yùn)行時(shí)自檢來選擇日志工具。MyBatis 日志工廠將使用它發(fā)現(xiàn)的第一個(gè)日志工具(按上面列舉的順序查找工具)。如果 MyBatis 一個(gè)都沒有找到哨苛,那么日志功能就會(huì)被禁用堕扶。
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:
很多環(huán)境把 Commons Logging 作為應(yīng)用服務(wù)器類路徑的一部分(比如 Tomcat 和 WebSphere)世落。有很重要的一點(diǎn)要知道潭辈,在這樣的環(huán)境中线罕,MyBatis 將使用 Commons Logging 作為日志工具羊瘩。這就意味著泰佳,在像 WebSphere 這樣的環(huán)境中盼砍,你的 Log4J 配置將會(huì)被忽略掉,因?yàn)?WebSphere 提供了 Commons Logging 的私有實(shí)現(xiàn)逝她。對(duì)于MyBatis 忽略你的 Log4J 配置這一點(diǎn)是非常煩人的(事實(shí)上浇坐,MyBatis 忽略你的 Log4J 配置是因?yàn)樵谶@樣的環(huán)境中它將使用 Commons Logging)。如果你的應(yīng)用運(yùn)行的是一個(gè)類路徑中包含 Commons Logging 的環(huán)境但你又想使用其他的日志工具黔宛,你可以通過在 mybatis-config.xml 文件中添加一行設(shè)置來選擇一個(gè)不同的日志工具:
<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 或一個(gè)實(shí)現(xiàn)了 org.apache.ibatis.logging.Log 并有一個(gè)帶 String 參數(shù)的構(gòu)造方法的類的完全限定類名近刘。
You can also select the implementation by calling one of the following methods:
你也可以選擇通過調(diào)用下面任意一個(gè)方法來選擇日志工具:
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.
如果你選擇調(diào)用其中一個(gè)方法,你應(yīng)該在調(diào)用其他 MyBatis 方法之前調(diào)用它臀晃。而且觉渴,僅在運(yùn)行期類路徑中存在該日志工具時(shí),這些方法才會(huì)轉(zhuǎn)換請(qǐng)求的日志工具徽惋。例如案淋,如果你試著選擇 Log4J 日志但是 Log4J 在運(yùn)行期不可用,那么 MyBatis 將會(huì)忽略使用 Log4J 的請(qǐng)求险绘,并且它將使用默認(rèn)算法來查找日志工具踢京。
通過下面鏈接獲取更多信息:
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 的日志語句,你需要對(duì)包宦棺、映射器的完全限定類名瓣距、命名空間、完全限定語句名稱開啟日志功能代咸。
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).
再次強(qiáng)調(diào)蹈丸,如何做取決于使用的日志工具。我們將展示如何使用 Log4J 來做呐芥。配置日志服務(wù)很簡(jiǎn)單:添加一個(gè)或多個(gè)配置文件(如 log4j.properties)逻杖,有時(shí)候需要添加一個(gè)新的 JAR 文件(如 log4j.jar)。
Step 1: 添加一個(gè) 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.
因?yàn)槲覀円褂?Log4J思瘟,我們將需要保證 JAR 包對(duì)我們的應(yīng)用是可用的弧腥。要使用 Log4J,你需要添加一個(gè) JAR 包到你的應(yīng)用類路徑潮太。你可以通過上面的 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.
對(duì)于 web 應(yīng)用或企業(yè)級(jí)應(yīng)用而言虾攻,你可以添加 log4j.jar 到你的 WEB-INF/lib 目錄铡买;對(duì)于獨(dú)立應(yīng)用你可以簡(jiǎn)單地將它添加到 JVM -classpath 的啟動(dòng)參數(shù)即可。
Step 2: 配置 Log4J
Configuring Log4J is simple. Suppose you want to enable the log for this mapper:
配置 Log4J 是很簡(jiǎn)單的霎箍。假設(shè)你要為這個(gè)映射器開啟日志:
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:
創(chuàng)建一個(gè)名為 log4j.properties 到你的類路徑中奇钞,具體內(nèi)容如下:
# 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 報(bào)告日志詳細(xì)信息且僅報(bào)告應(yīng)用中其他類的錯(cuò)誤信息。
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:
如果你想調(diào)整日志到一個(gè)更好的級(jí)別你可以為特定的語句打開日志功能而不是整個(gè)的映射器文件漂坏。下面這行配置將只為 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:
相反地景埃,你可能想要為一組映射器開啟日志功能媒至。在這種情況下,你應(yīng)該對(duì)映射器所在的包開啟日志:
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.
某些查詢可能返回大量的結(jié)果集谷徙。這種情況下你可能只想要查看 SQL 語句而不是結(jié)果拒啰。為了實(shí)現(xiàn)這個(gè)目的,可以將 SQL 語句的日志級(jí)別設(shè)置為 DEBUG(JDK 的日志設(shè)置為 FINE)完慧,結(jié)果的日志級(jí)別設(shè)置為 TRACE(JDK 日志設(shè)置為 FINER)谋旦。因此如果你想要查看語句而不查看結(jié)果,設(shè)置日志級(jí)別為 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:
這種情況下你可以通過為命名空間添加一個(gè) logger 來開啟整個(gè) XML 文件的日志功能:
log4j.logger.org.mybatis.example.BlogMapper=TRACE
Or for an specific statement:
或?yàn)橐粋€(gè)特定的語句:
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.
是的,就像你已經(jīng)注意到的脾歧,為映射器接口和 XML 映射文件配置日志沒什么不同甲捏。
NOTE If you are using SLF4J or Log4j 2 MyBatis will call it using the marker MYBATIS.
注意:如果你使用的是 SLF4J 或 Log4j 2,MyBatis 將以 MYBATIS 這個(gè)值來調(diào)用鞭执。
The remaining configuration in the log4j.properties file is used to configure the appenders. See Log4J website (URL above) for more information.
log4j.properties 文件中剩余的配置是用來配置日志輸出源的司顿。請(qǐng)查看 Log4J 官網(wǎng)獲取更多信息。
最后
說明:MyBatis 官網(wǎng)提供了簡(jiǎn)體中文的翻譯蚕冬,但個(gè)人覺得較為生硬免猾,甚至有些地方邏輯不通,于是自己一個(gè)個(gè)重新敲著翻譯的(都不知道哪里來的自信...)囤热,有些地方同官網(wǎng)翻譯有出入猎提,有些倔強(qiáng)地保留了自己的,有的實(shí)在別扭則保留了官網(wǎng)的旁蔼,這些都會(huì)在實(shí)踐中一一更正锨苏。鑒于個(gè)人英文能力有限,文章中保留了官方文檔原英文介紹(個(gè)別地方加以調(diào)整修剪)棺聊,希望有緣看到這里的朋友們能夠有自己的理解伞租,不會(huì)被我可能錯(cuò)誤或不合理的翻譯帶跑偏(〃'▽'〃),歡迎指正限佩!
當(dāng)前版本:mybatis-3.5.0
官網(wǎng)文檔:MyBatis
官網(wǎng)翻譯:MyBatis 簡(jiǎn)體中文
項(xiàng)目實(shí)踐:MyBatis Learn