Mybatis 文檔篇 7:Logging

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末葵诈,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子祟同,更是在濱河造成了極大的恐慌作喘,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,734評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件晕城,死亡現(xiàn)場(chǎng)離奇詭異泞坦,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)砖顷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門贰锁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來赃梧,“玉大人,你說我怎么就攤上這事豌熄∈卩郑” “怎么了?”我有些...
    開封第一講書人閱讀 164,133評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵房轿,是天一觀的道長(zhǎng)粤攒。 經(jīng)常有香客問我,道長(zhǎng)囱持,這世上最難降的妖魔是什么夯接? 我笑而不...
    開封第一講書人閱讀 58,532評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮纷妆,結(jié)果婚禮上盔几,老公的妹妹穿的比我還像新娘。我一直安慰自己掩幢,他們只是感情好逊拍,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,585評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著际邻,像睡著了一般芯丧。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上世曾,一...
    開封第一講書人閱讀 51,462評(píng)論 1 302
  • 那天缨恒,我揣著相機(jī)與錄音,去河邊找鬼轮听。 笑死骗露,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的血巍。 我是一名探鬼主播萧锉,決...
    沈念sama閱讀 40,262評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼述寡!你這毒婦竟也來了柿隙?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,153評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤鲫凶,失蹤者是張志新(化名)和其女友劉穎优俘,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體掀序,經(jīng)...
    沈念sama閱讀 45,587評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,792評(píng)論 3 336
  • 正文 我和宋清朗相戀三年惭婿,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了不恭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片叶雹。...
    茶點(diǎn)故事閱讀 39,919評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖换吧,靈堂內(nèi)的尸體忽然破棺而出折晦,到底是詐尸還是另有隱情,我是刑警寧澤沾瓦,帶...
    沈念sama閱讀 35,635評(píng)論 5 345
  • 正文 年R本政府宣布满着,位于F島的核電站,受9級(jí)特大地震影響贯莺,放射性物質(zhì)發(fā)生泄漏风喇。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,237評(píng)論 3 329
  • 文/蒙蒙 一缕探、第九天 我趴在偏房一處隱蔽的房頂上張望魂莫。 院中可真熱鬧,春花似錦爹耗、人聲如沸耙考。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽倦始。三九已至,卻和暖如春山卦,著一層夾襖步出監(jiān)牢的瞬間鞋邑,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評(píng)論 1 269
  • 我被黑心中介騙來泰國打工怒坯, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留炫狱,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,048評(píng)論 3 370
  • 正文 我出身青樓剔猿,卻偏偏與公主長(zhǎng)得像视译,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子归敬,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,864評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,331評(píng)論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,497評(píng)論 0 23
  • 我是馬良用左手教出來的徒弟
    諾墨的魚尾閱讀 353評(píng)論 1 9
  • 01 用旅行穿插著辭舊迎新的氣氛確實(shí)別致酷含。2016.12.31-2017.1.2紹興秀山島之行在舟車勞頓中結(jié)束,在...
    蘭風(fēng)蕙露閱讀 749評(píng)論 6 11
  • 奴顏媚骨仰望姿汪茧, 耀燿星子現(xiàn)天空椅亚, 卿卿漫筆予談詞, 此文卓然腐心靈舱污, 情深不予涼薄記呀舔, 沉迷不惹紅塵思, 執(zhí)手傳...
    安雁濃閱讀 181評(píng)論 0 0