1

使用 log4j1,我能够控制单个记录器级别,但属性语句似乎不适用于 log4j2。我将 rootLogger 级别设置为 DEBUG,并希望通过 commons-logging 抑制来自 Authorize.net 的大量调试日志。我正在尝试从 http 标头和线路日志中将级别设置为 WARN。

是详细配置记录器的 log4j 文档 (参考“配置 Log4J”)

这是一个示例日志输出:

2019-01-01 17:00:01 EST DEBUG org.apache.http.headers http-outgoing-0 << Content-Type: application/xml; charset=utf-8
2019-01-01 17:00:01 EST DEBUG org.apache.http.headers http-outgoing-0 << X-OPNET-Transaction-Trace: a2_5ed1139b-1533-463c-811d-a5ed796d44e7-11076-21806894
2019-01-01 17:00:01 EST DEBUG org.apache.http.headers http-outgoing-0 << Access-Control-Allow-Credentials: true
2019-01-01 17:00:01 EST DEBUG org.apache.http.headers http-outgoing-0 << Access-Control-Allow-Headers: x-requested-with,cache-control,content-type,origin,method,SOAPAction

2019-01-01 17:05:31 EST DEBUG org.apache.http.wire http-outgoing-0 >> "    <transactionRequest>[\n]"
2019-01-01 17:05:31 EST DEBUG org.apache.http.wire http-outgoing-0 >> "        <transactionType>authCaptureTransaction</transactionType>[\n]"
2019-01-01 17:05:31 EST DEBUG org.apache.http.wire http-outgoing-0 >> "        <amount>500</amount>[\n]"
2019-01-01 17:05:31 EST DEBUG org.apache.http.wire http-outgoing-0 >> "        <payment>[\n]"
2019-01-01 17:05:31 EST DEBUG org.apache.http.wire http-outgoing-0 >> "            <creditCard>[\n]"
2019-01-01 17:05:31 EST DEBUG org.apache.http.wire http-outgoing-0 >> "                <cardNumber>4242424242424242</cardNumber>[\n]"
2019-01-01 17:05:31 EST DEBUG org.apache.http.wire http-outgoing-0 >> "                <expirationDate>0822</expirationDate>[\n]"
2019-01-01 17:05:31 EST DEBUG org.apache.http.wire http-outgoing-0 >> "            </creditCard>[\n]"
2019-01-01 17:05:31 EST DEBUG org.apache.http.wire http-outgoing-0 >> "        </payment>[\n]"
2019-01-01 17:05:31 EST DEBUG org.apache.http.wire http-outgoing-0 >> "    </transactionRequest>[\n]"

这是我当前的 log4j2.properties 文件:

# Tell the root logger what appenders and level to use
rootLogger.level = DEBUG

rootLogger.appenderRefs = console, file
rootLogger.appenderRef.console.ref = console
rootLogger.appenderRef.file.ref = file

appenders = console, file


##### Authorize.net #####

# Controls detailed wire protocol
log4j.logger.org.apache.http.wire=WARN

# Controls headers (good for debugging)
log4j.logger.org.apache.http.headers=WARN

# Controls http context (what you are sending and geting)
log4j.logger.org.apache.http=WARN

# Controls htmlunit details
log4j.logger.com.gargoylesoftware.htmlunit=WARN


##### Console Appender #####

appender.console.name = console
appender.console.type = Console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss z} %-5p %c %m%n


##### File Appender #####

appender.file.name = file
appender.file.type = File
appender.file.fileName = mylog.log
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %d{yyyy-MM-dd HH:mm:ss z} %-5p %c %m%n
4

1 回答 1

1

因此,这显然是在 .properties 文件中使用或指定记录器配置的问题。我切换到新的 log4j2.xml 格式并让它完美运行。

提示:在 XML 文件中,您可以为记录器指定根名称,这些设置将应用于所有子分类记录器。举个例子

<Logger name="org.apache.http">

将适用于任何具有该根的记录器,例如在我原来的问题中:

<Logger name="org.apache.http.wire">
            - and - 
<Logger name="org.apache.http.headers">

这是抑制 Authorize.net 的所有调试消息的工作 log4j2.xml 文件。它是覆盖根记录器级别的一个很好的例子:

<?xml version="1.0" encoding="UTF-8"?>

<Configuration>

  <Appenders>
    <Console name="console">
      <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss z} %-5p %-5level %logger - %msg%n"/>
    </Console>

    <File name="file" fileName="mylogfile.log" append="true">
      <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss z} %-5p %-5level %logger - %msg%n"/>
    </File >
  </Appenders>


  <Loggers>
    <Root level="DEBUG">
      <AppenderRef ref="console"/>
      <AppenderRef ref="file"/>
    </Root>

    <Logger name="org.apache.http" level="WARN" additivity="false">
      <AppenderRef ref="console"/>
      <AppenderRef ref="file"/>
    </Logger>

    <Logger name="net.authorize.util" level="WARN" additivity="false">
      <AppenderRef ref="console"/>
      <AppenderRef ref="file"/>
    </Logger>

    <Logger name="net.authorize.api" level="WARN" additivity="false">
      <AppenderRef ref="console"/>
      <AppenderRef ref="file"/>
    </Logger>

  </Loggers>

</Configuration>
于 2019-01-02T18:44:23.240 回答