为什么log4j没有按预期运行?

时间:2010-03-19 15:25:10

标签: configuration log4j ibatis

我有一个同事试图让log4j表现如下:

  • 登录Stdout
  • 默认情况下,禁用大多数输出​​
  • 在级别调试时向上显示来自java.sql.PrepareStatement的消息

他陷入了“水平”与“优先”的关系。这是他的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "D:/Java/apache-log4j-1.2.15/src/main/resources/org/apache/log4j/xml/log4j.dtd" >
<log4j:configuration> 

    <!--  Appenders -->
    <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%5p %d{ISO8601} [%t][%x] %c - %m%n" />
        </layout>
    </appender>

    <!--  Loggers for ibatus and JDBC database -->
    <logger name="java.sql.PreparedStatement">
        <level value="debug"/>
    </logger>

    <!--  The Root Logger -->
    <root>
        <level value="error"/>
        <appender-ref ref="stdout"/>
    </root>

</log4j:configuration>

如图所示的更简单配置(根日志级别=错误):

log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [java.sql.PreparedStatement] additivity to [true].
log4j: Level value for java.sql.PreparedStatement is  [debug].
log4j: java.sql.PreparedStatement level set to DEBUG
log4j: Level value for root is  [error].
log4j: root level set to ERROR
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%5p %d{ISO8601} [%t][%x] %c - %m%n].
log4j: Adding appender named [stdout] to category [root].

将根日志级别的配置更改为调试(用...替换查询)

log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [java.sql.PreparedStatement] additivity to [true].
log4j: Level value for java.sql.PreparedStatement is  [debug].
log4j: java.sql.PreparedStatement level set to DEBUG
log4j: Level value for root is  [debug].
log4j: root level set to DEBUG
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%5p %d{ISO8601} [%t][%x] %c - %m%n].
log4j: Adding appender named [stdout] to category [root].
DEBUG 2010-03-19 12:59:58,256 [main][] com.ibatis.common.jdbc.SimpleDataSource - Created connection 1309601.
DEBUG 2010-03-19 12:59:58,256 [main][] java.sql.Connection - {conn-100000} Connection
DEBUG 2010-03-19 12:59:58,256 [main][] java.sql.Connection - {conn-100000} Preparing Statement:      …
DEBUG 2010-03-19 12:59:58,287 [main][] java.sql.PreparedStatement - {pstm-100001} Executing Statement:      …
DEBUG 2010-03-19 12:59:58,287 [main][] java.sql.PreparedStatement - {pstm-100001} Parameters: [%ATL]
DEBUG 2010-03-19 12:59:58,287 [main][] java.sql.PreparedStatement - {pstm-100001} Types: [java.lang.String]
DEBUG 2010-03-19 12:59:58,366 [main][] java.sql.ResultSet - {rset-100002} ResultSet
DEBUG 2010-03-19 12:59:58,381 [main][] java.sql.ResultSet - {rset-100002} Header: …
DEBUG 2010-03-19 12:59:58,381 [main][] java.sql.ResultSet - {rset-100002} Result: …
DEBUG 2010-03-19 12:59:58,381 [main][] java.sql.ResultSet - {rset-100002} Result: …
DEBUG 2010-03-19 12:59:58,381 [main][] java.sql.ResultSet - {rset-100002} Result: …
DEBUG 2010-03-19 12:59:58,397 [main][] com.ibatis.common.jdbc.SimpleDataSource - Returned connection 1309601 to pool.

他如何更改log4j.xml配置文件以使其表现如他所期望的那样?

3 个答案:

答案 0 :(得分:3)

我查看了mybatis的源代码,如下所示。您必须在java.sql.Connection上启用DEBUG才能执行java.sql.PreparedStatement的日志记录。我整天都在努力奋斗!

private Connection wrapConnection(Connection connection) {
    if (log.isDebugEnabled()) {
          return ConnectionLogger.newInstance(connection);
    } else {
      return connection;
    }
}

答案 1 :(得分:2)

我认为没有日志消息的原因是您想要查看日志的代码,不使用java.sql.PrepareStatement记录器,而是使用不同的记录器。记录器通常(尽管不是必需的)以使用它们的类命名。即com.ibatis.SomeClass通常使用java.sql.PrepareStatement记录器。

将根记录器设置为DEBUG,并查看为您提供所需消息的记录器名称。然后使用DEBUG配置这些记录器,并让root目录仅处于ERROR级别。

顺便说一下,它是Prepare_d_Statement,它是接口(即那里没有日志代码),它肯定不使用log4j,因为它在JDK中。

答案 2 :(得分:2)

在log4j.xml中插入以下内容:

<!-- *******************************************************
     WARNING: iBatus 2.3.3 (only ver. tested) is a little weird. 
             YOU MUST SET
             java.sql.Connection to debug to get any 
             java.sql.PreparedStatement debug logs.
     ************************************************************** -->       
<logger name="java.sql.Connection" additivity="false">
    <level value="debug"/>
</logger>

DEBUG 2010-03-19 14:27:08,425 [main] [] [java.sql.PreparedStatement] - {pstm-100001}执行声明:
...