如何在我的项目中的log4j2中显示数据库查询?

时间:2016-03-14 08:01:43

标签: logging log4j2

我想在我的项目中显示我的查询日志。 我设置了我的文件(log4j2.xml),但我没有表明。

log4j2.xml是

<Configuration>
<Appenders>
    <Console name="console" target="SYSTEM_OUT">
        <!--<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>-->
        <PatternLayout pattern="%d %5p [%c] %m%n" />
    </Console>
    <Console name="console-mamoru" target="SYSTEM_OUT">
        <PatternLayout pattern="[MAMORU] (%d) [%5p] [%c] %m%n" />
    </Console>
    <File name="file-mamoru" fileName="C:/logs/springweb/filelog.log" append="false">
        <PatternLayout pattern="[MAMORU-FILELOG] (%d) [%5p] [%c] %m%n" />
    </File>
</Appenders>
<Loggers>
    <Logger name="org.springframework" level="INFO" additivity="false">
        <AppenderRef ref="console" />
    </Logger>
    <Logger name="java.sql." level="INFO" additivity="false">
        <AppenderRef ref="console" />
    </Logger>
    <Logger name="com.mamoru" level="DEBUG" additivity="false">
        <AppenderRef ref="console-mamoru"/>
        <AppenderRef ref="file-mamoru"/>
    </Logger>
    <Root level="DEBUG">
        <AppenderRef ref="console"/>
    </Root>
</Loggers>

我的日志是:

  

2016-03-14 16:33:29,228 INFO [org.springframework.test.context.support.DefaultTestContextBootstrapper]从位置[META-INF / spring.factories]加载默认的TestExecutionListener类名:[org.springframework.test .context.web.ServletTestExecutionListener,org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener,org.springframework.test.context.support.DependencyInjectionTestExecutionListener,org.springframework.test.context.support.DirtiesContextTestExecutionListener,org.springframework.test.context .transaction.TransactionalTestExecutionListener,org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]   2016-03-14 16:33:29,239 INFO [org.springframework.test.context.support.DefaultTestContextBootstrapper]无法实例化TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener]。指定自定义侦听器类或使默认侦听器类(及其所需的依赖项)可用。违规类:[org / springframework / transaction / interceptor / TransactionAttributeSource]   2016-03-14 16:33:29,246 INFO [org.springframework.test.context.support.DefaultTestContextBootstrapper]无法实例化TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]。指定自定义侦听器类或使默认侦听器类(及其所需的依赖项)可用。违规类:[org / springframework / transaction / interceptor / TransactionAttribute]   2016-03-14 16:33:29,249 INFO [org.springframework.test.context.support.DefaultTestContextBootstrapper]使用TestExecutionListeners:[org.springframework.test.context.web.ServletTestExecutionListener@19aeb7a6,org.springframework.test.context。 support.DirtiesContextBeforeModesTestExecutionListener@1d9b0076,org.springframework.test.context.support.DependencyInjectionTestExecutionListener @ 2b3b5229,org.springframework.test.context.support.DirtiesContextTestExecutionListener@438f17eb] 2016-03-14 16:33:29,558 INFO [org.springframework。 beans.factory.xml.XmlBeanDefinitionReader]从URL加载XML bean定义[file:/ C:/mamoru/github/spring/springweb/target/classes/spring/root-context.xml]   2016-03-14 16:33:30,747 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]从URL加载XML bean定义[file:/ C:/ mamoru / github / spring / springweb / target / classes / spring /springwebServlet/servlet-context-common.xml]   2016-03-14 16:33:31,080 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]从URL加载XML bean定义[file:/ C:/ mamoru / github / spring / springweb / target / classes / spring /springwebServlet/servlet-context-datasource.xml]   2016-03-14 16:33:31,120 INFO [org.springframework.context.support.GenericApplicationContext]刷新org.springframework.context.support.GenericApplicationContext@40f69ae7:启动日期[Mon Mar 14 16:33:31 KST 2016];上下文层次结构的根   2016-03-14 16:33:31,652 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] Mapped&#34; {[/ helloSpring]}&#34;到公共org.springframework.web.servlet.ModelAndView com.mamoru.webapp.hellospring.controller.HelloSpringController.hello()   2016-03-14 16:33:31,871 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]寻找@ControllerAdvice:org.springframework.context.support.GenericApplicationContext@40f69ae7:启动日期[Mon Mar 14 16:33:31 KST 2016];上下文层次结构的根   2016-03-14 16:33:31,964 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]寻找@ControllerAdvice:org.springframework.context.support.GenericApplicationContext@40f69ae7:启动日期[Mon Mar 14 16:33:31 KST 2016];上下文层次结构的根   2016-03-14 16:33:32,090 INFO [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping]将映射的URL路径[/ resources / **]映射到处理程序&org.springframework.web.servlet.resource。 ResourceHttpRequestHandler#0&#39;   [MAMORU](2016-03-14 16:33:32,124)[DEBUG] [com.mamoru.common.database.DBConnector] [DBConnector构造函数加载]   [MAMORU](2016-03-14 16:33:32,136)[DEBUG] [com.mamoru.common.database.DBConnector] [Init DB Connector] START =============== ============================================   [MAMORU](2016-03-14 16:33:32,212)[DEBUG] [com.mamoru.common.database.DBConnector] [Init DB Connector] END =============== ==============================================   [MAMORU](2016-03-14 16:33:33,298)[DEBUG] [com.mamoru.common.database.DBConnectorTest] [数据列表] OBJECTID   [MAMORU](2016-03-14 16:33:33,299)[DEBUG] [com.mamoru.common.database.DBConnectorTest] [数据列表] REV   [MAMORU](2016-03-14 16:33:33,299)[DEBUG] [com.mamoru.common.database.DBConnectorTest] [数据列表] LINKTYPE   [MAMORU](2016-03-14 16:33:33,299)[DEBUG] [com.mamoru.common.database.DBConnectorTest] [数据列表] LINKQUALIFIER   [MAMORU](2016-03-14 16:33:33,299)[DEBUG] [com.mamoru.common.database.DBConnectorTest] [数据列表] FIRSTID   [MAMORU](2016-03-14 16:33:33,299)[DEBUG] [com.mamoru.common.database.DBConnectorTest] [数据列表] SECONDID   2016-03-14 16:33:33,302 INFO [org.springframework.context.support.GenericApplicationContext]关闭org.springframework.context.support.GenericApplicationContext@40f69ae7:启动日期[Mon Mar 14 16:33:31 KST 2016];上下文层次结构的根

如何显示查询日志?

1 个答案:

答案 0 :(得分:0)

可能您的数据库框架将查询记录为INFO(如hibernate所做)。所以修改你的log4j-config:

<!-- ... -->
    <Logger name="com.mamoru" level="INFO" additivity="false">
            <AppenderRef ref="console-mamoru"/>
            <AppenderRef ref="file-mamoru"/>
    </Logger>
<!-- ... -->