如何在Log4j2中设置特定的记录器级别

时间:2019-01-01 22:14:25

标签: log4j2 authorize.net apache-commons-logging log-level

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

这里是log4j doc that details configuring a logger(参考“配置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

1 个答案:

答案 0 :(得分:0)

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

提示:在XML文件中,您可以为记录器指定一个根名称,并且该设置将应用于所有子类的记录器。例如

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

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

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

这是有效的log4j2.xml文件,该文件将覆盖Authorize.net的所有调试消息。这是覆盖根记录程序级别的一个很好的例子:

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