如何在 Log4j2 中设置特定的记录器级别
How To Set Specific Logger Level in Log4j2
使用 log4j1 我能够控制单个记录器级别,但 属性 语句似乎不适用于 log4j2。我将 rootLogger 级别设置为 DEBUG,并希望通过 commons-logging 抑制来自 Authorize.net 的大量调试日志。我正在尝试从 http headers 和线路日志中将级别设置为 WARN。
这里是log4j doc that details configuring a logger(参考"Configuring 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
所以这显然是在 .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>
使用 log4j1 我能够控制单个记录器级别,但 属性 语句似乎不适用于 log4j2。我将 rootLogger 级别设置为 DEBUG,并希望通过 commons-logging 抑制来自 Authorize.net 的大量调试日志。我正在尝试从 http headers 和线路日志中将级别设置为 WARN。
这里是log4j doc that details configuring a logger(参考"Configuring 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
所以这显然是在 .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>