痛苦了好幾天,新項目的環境是jboss as 7.1.1 final + mybatis 3.2.2 +spring4.0.0java
今天要輸出mybatis的日誌,以便查看所執行的SQL語句,不管如何配置log4j.properties都沒法實現。web
按照mybatis的文檔提供的方法進行log4j.properties文件以及mybatis-config.xml文件的設置,仍是不能輸出sql。感受好像本身寫的log4j.properties文件沒有生效同樣。spring
在網上搜素了一些資源:sql
http://kang36897.blog.163.com/blog/static/17047373201211262204229/apache
有的說,在pom.xml文件中爲log4j加上<scope>provided</scope>,有的說在web.xml中配置log4j的listener,絕大部分的文檔是要你們修改log4j.properties,相信我,絕對沒用。tomcat
後來忽然想到,會不會是容器的問題。改用junit直接測試mapper類,發現能夠正常輸出SQL日誌。由此開始懷疑是jboss as 7.1.1的問題。又在網上搜索了一些文章,好比:https://community.jboss.org/thread/198196,我在項目中增長了jboss-deployment-structure.xml仍是不行。mybatis
偶然在jboss的standalone.xml文件中發現了jboss本身的日誌配置。通過修改其配置,終於能夠正常輸出了。app
<subsystem xmlns="urn:jboss:domain:logging:1.1"> <console-handler name="CONSOLE"> <level name="INFO"/> <formatter> <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/> </formatter> </console-handler> <periodic-rotating-file-handler name="FILE"> <formatter> <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/> </formatter> <file relative-to="jboss.server.log.dir" path="server.log"/> <suffix value=".yyyy-MM-dd"/> <append value="true"/> </periodic-rotating-file-handler> <logger category="com.arjuna"> <level name="WARN"/> </logger> <logger category="org.apache.tomcat.util.modeler"> <level name="WARN"/> </logger> <logger category="sun.rmi"> <level name="WARN"/> </logger> <logger category="jacorb"> <level name="WARN"/> </logger> <logger category="jacorb.config"> <level name="ERROR"/> </logger> <root-logger> <level name="INFO"/> <handlers> <handler name="CONSOLE"/> <handler name="FILE"/> </handlers> </root-logger> </subsystem>
把以上的代碼修改成以下的內容:dom
<subsystem xmlns="urn:jboss:domain:logging:1.1"> <console-handler name="CONSOLE"> <!-- <level name="INFO"/> ----- 取消console的默認輸出級別--> <formatter> <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/> </formatter> </console-handler> <periodic-rotating-file-handler name="FILE"> <formatter> <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/> </formatter> <file relative-to="jboss.server.log.dir" path="server.log"/> <suffix value=".yyyy-MM-dd"/> <append value="true"/> </periodic-rotating-file-handler> <logger category="com.arjuna"> <level name="WARN"/> </logger> <logger category="org.apache.tomcat.util.modeler"> <level name="WARN"/> </logger> <logger category="sun.rmi"> <level name="WARN"/> </logger> <logger category="jacorb"> <level name="WARN"/> </logger> <logger category="jacorb.config"> <level name="ERROR"/> </logger> <!-- 修改mybatis的mapper類文件的輸出級別。這裏category配置的是本身項目的包路徑 --> <logger category="cn.com.youcompany.projectname.dao"> <level name="DEBUG"/> </logger> <!------------------------------------------> <root-logger> <level name="INFO"/> <handlers> <handler name="CONSOLE"/> <handler name="FILE"/> </handlers> </root-logger> </subsystem>
正確的輸入內容以下:ide
17:03:46,271 DEBUG [cn.com.xxx.dao.PermissionMapper.getMenuItemList] (http--127.0.0.1-8080-2) ooo Using Connection [org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6@135f74] 17:03:46,272 DEBUG [cn.com.xxx.dao.PermissionMapper.getMenuItemList] (http--127.0.0.1-8080-2) ==> Preparing: select permission_id, permission_name, parent_permission_id, permission_url, level permission_level from ( select distinct p.permission_id, p.permission_name, p.parent_permission_id, p.permission_url, p.orderby from permission p, permission_role pr, user_role ur where ur.user_login_id = ? and ur.role_type_id = pr.role_id and pr.permission_id = p.permission_id and p.is_menu = '1' order by p.orderby ) t connect by prior permission_id = parent_permission_id start with parent_permission_id = '-1' 17:03:46,274 DEBUG [cn.com.xxx.dao.PermissionMapper.getMenuItemList] (http--127.0.0.1-8080-2) ==> Parameters: administrator(String) 17:03:46,292 DEBUG [cn.com.xxx.dao.PermissionMapper.getMenuItemList] (http--127.0.0.1-8080-2) <== Total: 4
分析緣由:默認的jboss as 711的console的日誌輸出級別是:INFO。而從mybatis的文檔要求上來看,必需要debug級別日誌輸出才能夠輸出SQL語句。所以Jboss as 的日誌輸出級別覆蓋了mybatis的日誌輸出內容。去掉其默認配置,而且按照mybatias的文檔要求,設置本身項目類的包的日誌輸出級別是debug,全部問題都解決了。
不須要修改pom.xml文件,不須要在web-inf下增長jboss-deployment-structure.xml文件。
個人log4j的配置文件內容以下:
log4j.rootLogger=INFO,CONSOLE,ROLLING_FILE log4j.additivity.org.apache=true #console output log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Threshold=DEBUG log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n #file output log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender log4j.appender.ROLLING_FILE.Threshold=ERROR log4j.appender.ROLLING_FILE.File=log/votp.log log4j.appender.ROLLING_FILE.Append=true log4j.appender.ROLLING_FILE.MaxFileSize=10240KB log4j.appender.ROLLING_FILE.MaxBackupIndex=1 log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n