log4j2authorize.netapache-commons-logginglog-level

How To Set Specific Logger Level in Log4j2


With log4j1 I was able to control an individual logger level but the property statements appear to not be working with log4j2. I have the rootLogger level set to DEBUG and want to suppress the numerous debug logs coming from Authorize.net via commons-logging. I'm trying to set the level to WARN from the http headers and wire logs.

Here's the log4j doc that details configuring a logger (reference "Configuring Log4J")

This is a sample log output:

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

This is my current log4j2.properties file:

# 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

Solution

  • So this apparently turns out to be a problem with using or specifying logger configs in a .properties file. I switched to the new log4j2.xml format and have it working perfectly.

    Tip: In the XML file you can specify a root name for a logger and the settings will apply to all sub-classed loggers. As an example

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

    Will apply to any logger that has that root such as in my original question:

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

    Here is the working log4j2.xml file that supresses all debug messages for Authorize.net. Its a good example of overriding the root logger level:

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