2013-06-06 21:23 391人阅读 评论(0) 收藏 举报
项目使用Spring3+MyBatis3整合,但是控制台就是部署出SQL语句,网上找了很多资料,都说是Log4j配置文件的问题,修改了N 边Log4j配置文件依然无效。
除了SQL 语句,其他都能显示,纠结的很
我的Log4j的配置文件如下:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPElog4j:configurationSYSTEM"http://toolkit.alibaba-inc.com/dtd/log4j/log4j.dtd">
- <log4j:configurationxmlns:log4j='http://jakarta.apache.org/log4j/'>
- <!--=====================================================================-->
- <!--定义全局输出适配器-->
- <!--=====================================================================-->
- <appendername="consoleAppender"class="org.apache.log4j.ConsoleAppender">
- <layoutclass="org.apache.log4j.PatternLayout">
- <paramname="ConversionPattern"value="[%d{yyyy-MM-ddHH:mm:ss.SSS}]-[%c]-[%t]%x-%m%n"/>
- </layout>
- </appender>
- <!--MybatisandSQL-->
- <loggername="org.apache.ibatis"additivity="true">
- <levelvalue="DEBUG"/>
- <appender-refref="consoleAppender"/>
- </logger>
- <loggername="org.mybatis.spring"additivity="true">
- <levelvalue="ERROR"/>
- <appender-refref="consoleAppender"/>
- </logger>
- <loggername="java.sql"additivity="true">
- <levelvalue="DEBUG"/>
- <appender-refref="consoleAppender"/>
- </logger>
- <loggername="java.sql.Connection"additivity="true">
- <levelvalue="DEBUG"/>
- <appender-refref="consoleAppender"/>
- </logger>
- <loggername="java.sql.Statement"additivity="true">
- <levelvalue="DEBUG"/>
- <appender-refref="consoleAppender"/>
- </logger>
- <loggername="java.sql.PreparedStatement"additivity="true">
- <levelvalue="DEBUG"/>
- <appender-refref="consoleAppender"/>
- </logger>
- <!--Spring-->
- <loggername="org.springframework"additivity="true">
- <levelvalue="ERROR"/>
- <appender-refref="consoleAppender"/>
- </logger>
- <!--ActiveMQ-->
- <loggername="org.apache.activemq"additivity="true">
- <levelvalue="ERROR"/>
- <appender-refref="consoleAppender"/>
- </logger>
- <!--=====================================================================-->
- <!--根日志记录器-->
- <!--=====================================================================-->
- <root>
- <levelvalue="DEBUG"/>
- <appender-refref="consoleAppender"/>
- </root>
- </log4j:configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "http://toolkit.alibaba-inc.com/dtd/log4j/log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<!-- ===================================================================== -->
<!-- 定义全局输出适配器 -->
<!-- ===================================================================== -->
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss.SSS}]-[%c]-[%t] %x - %m%n"/>
</layout>
</appender>
<!-- Mybatis and SQL -->
<logger name="org.apache.ibatis" additivity="true">
<level value="DEBUG"/>
<appender-ref ref="consoleAppender"/>
</logger>
<logger name="org.mybatis.spring" additivity="true">
<level value="ERROR"/>
<appender-ref ref="consoleAppender"/>
</logger>
<logger name="java.sql" additivity="true">
<level value="DEBUG"/>
<appender-ref ref="consoleAppender"/>
</logger>
<logger name="java.sql.Connection" additivity="true">
<level value="DEBUG"/>
<appender-ref ref="consoleAppender"/>
</logger>
<logger name="java.sql.Statement" additivity="true">
<level value="DEBUG"/>
<appender-ref ref="consoleAppender"/>
</logger>
<logger name="java.sql.PreparedStatement" additivity="true">
<level value="DEBUG"/>
<appender-ref ref="consoleAppender"/>
</logger>
<!-- Spring -->
<logger name="org.springframework" additivity="true">
<level value="ERROR"/>
<appender-ref ref="consoleAppender"/>
</logger>
<!-- ActiveMQ -->
<logger name="org.apache.activemq" additivity="true">
<level value="ERROR"/>
<appender-ref ref="consoleAppender"/>
</logger>
<!-- ===================================================================== -->
<!-- 根日志记录器 -->
<!-- ===================================================================== -->
<root>
<level value="DEBUG"/>
<appender-ref ref="consoleAppender"/>
</root>
</log4j:configuration>
最后,仔细查看启动时的输入发现了如下的错误提示:
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
于是,我想是不是这个地方出问题了呢,赶紧上网搜索解决这个问题的资料。网上找到一个解释:
地址:http://lveyo.iteye.com/blog/418376
这个提示应该是读入web应用程序的log4j.properties文件之前就报出来了。是在加载org.springframework.web.context.ContextLoader这个listener的时候没找到log4j的配置文件造成的。
仔细查看web.xml发现在加载org.springframework.web.context.ContextLoader这个listener之后才加载org.springframework.web.util.Log4jConfigListener,把log4j的配置放到org.springframework.web.context.ContextLoader之前,就可以解决这个问题了。
于是,我在web.xml配置文件中加入Log4j的配置,部分配置文件如下:
- <!--Log4j配置-->
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>/WEB-INF/log4j.xml</param-value>
- </context-param>
- <context-param>
- <param-name>log4jRefreshInterval</param-name>
- <param-value>60000</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
- <!--扫描spring配置文件-->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring/application-*.xml</param-value>
- </context-param>
- <!--监听器-->
- <listener>
- <listener-class>com.hanvon.iface.web.listener.WebAppEventListener</listener-class>
- </listener>
- .......
<!--Log4j配置-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- 扫描spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-*.xml</param-value>
</context-param>
<!-- 监听器 -->
<listener>
<listener-class>com.hanvon.iface.web.listener.WebAppEventListener</listener-class>
</listener>
.......
增加了上面的Log4j配置部分后,结果真的可以打印SQL 语句了,太高兴了!!!
我觉得,起作用的主要是下面的配置内容,这个配置正确设置了Log4j:
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>/WEB-INF/log4j.xml</param-value>
- </context-param>
- <context-param>
- <param-name>log4jRefreshInterval</param-name>
- <param-value>60000</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
部分SQL语句输出如下:
- [2013-06-0621:07:35.040]-[java.sql.PreparedStatement]-[http-8080-2]-==>Executing:SELECTCOUNT(DISTINCTdev.ng_id)FROMdev_devicedevWHEREdev.nt_state=?
- [2013-06-0621:07:35.040]-[java.sql.PreparedStatement]-[http-8080-2]-==>Executing:SELECTCOUNT(DISTINCTdev.ng_id)FROMdev_devicedevWHEREdev.nt_state=?
- [2013-06-0621:07:35.040]-[java.sql.PreparedStatement]-[http-8080-1]-==>Parameters:
- [2013-06-0621:07:35.040]-[java.sql.PreparedStatement]-[http-8080-1]-==>Parameters:
- [2013-06-0621:07:35.040]-[java.sql.PreparedStatement]-[http-8080-2]-==>Executing:SELECTCOUNT(DISTINCTdev.ng_id)FROMdev_devicedevWHEREdev.nt_state=?
- [2013-06-0621:07:35.040]-[java.sql.PreparedStatement]-[http-8080-1]-==>Parameters:
- [2013-06-0621:07:35.041]-[java.sql.PreparedStatement]-[http-8080-2]-==>Parameters:1(Integer)
- [2013-06-0621:07:35.041]-[java.sql.PreparedStatement]-[http-8080-2]-==>Parameters:1(Integer)
- [2013-06-0621:07:35.041]-[java.sql.PreparedStatement]-[http-8080-2]-==>Parameters:1(Integer)
- [2013-06-0621:07:35.079]-[java.sql.ResultSet]-[http-8080-2]-<==Columns:COUNT(DISTINCTdev.ng_id)
- [2013-06-0621:07:35.079]-[java.sql.ResultSet]-[http-8080-2]-<==Columns:COUNT(DISTINCTdev.ng_id)
- [2013-06-0621:07:35.087]-[java.sql.ResultSet]-[http-8080-2]-<==Row:11
- [2013-06-0621:07:35.087]-[java.sql.ResultSet]-[http-8080-2]-<==Row:11
项目使用Spring3+MyBatis3整合,但是控制台就是部署出SQL语句,网上找了很多资料,都说是Log4j配置文件的问题,修改了N 边Log4j配置文件依然无效。
除了SQL 语句,其他都能显示,纠结的很
我的Log4j的配置文件如下:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPElog4j:configurationSYSTEM"http://toolkit.alibaba-inc.com/dtd/log4j/log4j.dtd">
- <log4j:configurationxmlns:log4j='http://jakarta.apache.org/log4j/'>
- <!--=====================================================================-->
- <!--定义全局输出适配器-->
- <!--=====================================================================-->
- <appendername="consoleAppender"class="org.apache.log4j.ConsoleAppender">
- <layoutclass="org.apache.log4j.PatternLayout">
- <paramname="ConversionPattern"value="[%d{yyyy-MM-ddHH:mm:ss.SSS}]-[%c]-[%t]%x-%m%n"/>
- </layout>
- </appender>
- <!--MybatisandSQL-->
- <loggername="org.apache.ibatis"additivity="true">
- <levelvalue="DEBUG"/>
- <appender-refref="consoleAppender"/>
- </logger>
- <loggername="org.mybatis.spring"additivity="true">
- <levelvalue="ERROR"/>
- <appender-refref="consoleAppender"/>
- </logger>
- <loggername="java.sql"additivity="true">
- <levelvalue="DEBUG"/>
- <appender-refref="consoleAppender"/>
- </logger>
- <loggername="java.sql.Connection"additivity="true">
- <levelvalue="DEBUG"/>
- <appender-refref="consoleAppender"/>
- </logger>
- <loggername="java.sql.Statement"additivity="true">
- <levelvalue="DEBUG"/>
- <appender-refref="consoleAppender"/>
- </logger>
- <loggername="java.sql.PreparedStatement"additivity="true">
- <levelvalue="DEBUG"/>
- <appender-refref="consoleAppender"/>
- </logger>
- <!--Spring-->
- <loggername="org.springframework"additivity="true">
- <levelvalue="ERROR"/>
- <appender-refref="consoleAppender"/>
- </logger>
- <!--ActiveMQ-->
- <loggername="org.apache.activemq"additivity="true">
- <levelvalue="ERROR"/>
- <appender-refref="consoleAppender"/>
- </logger>
- <!--=====================================================================-->
- <!--根日志记录器-->
- <!--=====================================================================-->
- <root>
- <levelvalue="DEBUG"/>
- <appender-refref="consoleAppender"/>
- </root>
- </log4j:configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "http://toolkit.alibaba-inc.com/dtd/log4j/log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<!-- ===================================================================== -->
<!-- 定义全局输出适配器 -->
<!-- ===================================================================== -->
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss.SSS}]-[%c]-[%t] %x - %m%n"/>
</layout>
</appender>
<!-- Mybatis and SQL -->
<logger name="org.apache.ibatis" additivity="true">
<level value="DEBUG"/>
<appender-ref ref="consoleAppender"/>
</logger>
<logger name="org.mybatis.spring" additivity="true">
<level value="ERROR"/>
<appender-ref ref="consoleAppender"/>
</logger>
<logger name="java.sql" additivity="true">
<level value="DEBUG"/>
<appender-ref ref="consoleAppender"/>
</logger>
<logger name="java.sql.Connection" additivity="true">
<level value="DEBUG"/>
<appender-ref ref="consoleAppender"/>
</logger>
<logger name="java.sql.Statement" additivity="true">
<level value="DEBUG"/>
<appender-ref ref="consoleAppender"/>
</logger>
<logger name="java.sql.PreparedStatement" additivity="true">
<level value="DEBUG"/>
<appender-ref ref="consoleAppender"/>
</logger>
<!-- Spring -->
<logger name="org.springframework" additivity="true">
<level value="ERROR"/>
<appender-ref ref="consoleAppender"/>
</logger>
<!-- ActiveMQ -->
<logger name="org.apache.activemq" additivity="true">
<level value="ERROR"/>
<appender-ref ref="consoleAppender"/>
</logger>
<!-- ===================================================================== -->
<!-- 根日志记录器 -->
<!-- ===================================================================== -->
<root>
<level value="DEBUG"/>
<appender-ref ref="consoleAppender"/>
</root>
</log4j:configuration>
最后,仔细查看启动时的输入发现了如下的错误提示:
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
于是,我想是不是这个地方出问题了呢,赶紧上网搜索解决这个问题的资料。网上找到一个解释:
地址:http://lveyo.iteye.com/blog/418376
这个提示应该是读入web应用程序的log4j.properties文件之前就报出来了。是在加载org.springframework.web.context.ContextLoader这个listener的时候没找到log4j的配置文件造成的。
仔细查看web.xml发现在加载org.springframework.web.context.ContextLoader这个listener之后才加载org.springframework.web.util.Log4jConfigListener,把log4j的配置放到org.springframework.web.context.ContextLoader之前,就可以解决这个问题了。
于是,我在web.xml配置文件中加入Log4j的配置,部分配置文件如下:
- <!--Log4j配置-->
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>/WEB-INF/log4j.xml</param-value>
- </context-param>
- <context-param>
- <param-name>log4jRefreshInterval</param-name>
- <param-value>60000</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
- <!--扫描spring配置文件-->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring/application-*.xml</param-value>
- </context-param>
- <!--监听器-->
- <listener>
- <listener-class>com.hanvon.iface.web.listener.WebAppEventListener</listener-class>
- </listener>
- .......
<!--Log4j配置-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- 扫描spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-*.xml</param-value>
</context-param>
<!-- 监听器 -->
<listener>
<listener-class>com.hanvon.iface.web.listener.WebAppEventListener</listener-class>
</listener>
.......
增加了上面的Log4j配置部分后,结果真的可以打印SQL 语句了,太高兴了!!!
我觉得,起作用的主要是下面的配置内容,这个配置正确设置了Log4j:
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>/WEB-INF/log4j.xml</param-value>
- </context-param>
- <context-param>
- <param-name>log4jRefreshInterval</param-name>
- <param-value>60000</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
部分SQL语句输出如下:
- [2013-06-0621:07:35.040]-[java.sql.PreparedStatement]-[http-8080-2]-==>Executing:SELECTCOUNT(DISTINCTdev.ng_id)FROMdev_devicedevWHEREdev.nt_state=?
- [2013-06-0621:07:35.040]-[java.sql.PreparedStatement]-[http-8080-2]-==>Executing:SELECTCOUNT(DISTINCTdev.ng_id)FROMdev_devicedevWHEREdev.nt_state=?
- [2013-06-0621:07:35.040]-[java.sql.PreparedStatement]-[http-8080-1]-==>Parameters:
- [2013-06-0621:07:35.040]-[java.sql.PreparedStatement]-[http-8080-1]-==>Parameters:
- [2013-06-0621:07:35.040]-[java.sql.PreparedStatement]-[http-8080-2]-==>Executing:SELECTCOUNT(DISTINCTdev.ng_id)FROMdev_devicedevWHEREdev.nt_state=?
- [2013-06-0621:07:35.040]-[java.sql.PreparedStatement]-[http-8080-1]-==>Parameters:
- [2013-06-0621:07:35.041]-[java.sql.PreparedStatement]-[http-8080-2]-==>Parameters:1(Integer)
- [2013-06-0621:07:35.041]-[java.sql.PreparedStatement]-[http-8080-2]-==>Parameters:1(Integer)
- [2013-06-0621:07:35.041]-[java.sql.PreparedStatement]-[http-8080-2]-==>Parameters:1(Integer)
- [2013-06-0621:07:35.079]-[java.sql.ResultSet]-[http-8080-2]-<==Columns:COUNT(DISTINCTdev.ng_id)
- [2013-06-0621:07:35.079]-[java.sql.ResultSet]-[http-8080-2]-<==Columns:COUNT(DISTINCTdev.ng_id)
- [2013-06-0621:07:35.087]-[java.sql.ResultSet]-[http-8080-2]-<==Row:11
- [2013-06-0621:07:35.087]-[java.sql.ResultSet]-[http-8080-2]-<==Row:11