为什么 log4j 的行为不符合预期?

发布于 2024-08-26 08:13:56 字数 3643 浏览 4 评论 0原文

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

  • Log to 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配置文件以使其表现得像他一样期待?

I have a co-worker who is trying to get log4j to behave as follows:

  • Log to Stdout
  • By default, disable most output
  • Show only messages from java.sql.PrepareStatement at level debug and up

He's getting caught up in the 'level' vs 'priority'. Here is his config file:

<?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>

Simpler configuration as shown (Root Log Level = ERROR):

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].

Configuration with Root Log Level changed to debug (replace queries with …)

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.

How does he need to change his log4j.xml config file to make it behave as he's expecting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你在看孤独的风景 2024-09-02 08:13:56

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

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

I looked at the source code for mybatis, shown below. You have to enable DEBUG on java.sql.Connection in order for it execute the logging for java.sql.PreparedStatement. I struggled with this all day!

private Connection wrapConnection(Connection connection) {
    if (log.isDebugEnabled()) {
          return ConnectionLogger.newInstance(connection);
    } else {
      return connection;
    }
}
等往事风中吹 2024-09-02 08:13:56

将以下添加内容插入到 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} 执行语句:

...

Insert the following addition into your 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} Executing Statement:

...

慕烟庭风 2024-09-02 08:13:56

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

将根记录器设置为“调试”,然后查看为您提供所需消息的记录器的名称。然后使用 DEBUG 配置这些记录器,并让 root 仅在 ERROR 级别记录日志。

顺便说一句,它是Prepare_d_Statement,它是接口(即那里没有日志记录代码),并且它绝对不使用log4j,因为它在JDK中。

I think the reason there are no log messages is that code you want to see logs from, doesn't use java.sql.PrepareStatement logger, but different loggers. Loggers are usually (although not necessarilly) named after classes that use them. I.e. com.ibatis.SomeClass would typically not use java.sql.PrepareStatement logger.

Set your root logger to DEBUG, and check out names of loggers that give you messages you want. Then configure these loggers with DEBUG and let root log at ERROR level only.

Btw, it's Prepare_d_Statement, it is interface (i.e. no logging code there), and it definitely doesn't use log4j since it is in JDK.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文