【问题标题】:Can't turn down HtmlUnit logging无法关闭 HtmlUnit 日志记录
【发布时间】:2017-03-21 05:11:06
【问题描述】:

我正在使用 org.apache.log4j.Logger 并希望关闭 htmlunit 的调试消息。我所做的一切似乎都不起作用。我不断收到 Http.wire、Http.headers 等调试消息。我已将根记录器设置为调试

我尝试将这一行放在我的代码中:

org.apache.log4j.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(org.apache.log4j.Level.OFF);

我也尝试将这一行放在我的 log4j.properties 文件中:

log4j.logger.com.gargoylesoftware.htmlunit=WARN

这是我的 log4j.properties 文件的内容:

log4j.logger.com.gargoylesoftware.htmlunit=ERROR

log4j.logger.org.apache.commons.httpclient=ERROR


# Tell the root lodger what appenders and level to use
log4j.rootLogger=DEBUG, A1, A2


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

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n


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

log4j.appender.A2=org.apache.log4j.FileAppender
log4j.appender.A2.File=/var/log/mylogfile.log
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n
log4j.appender.A2.Append=false

任何帮助将不胜感激。

编辑 11/15/16(添加测试代码)

import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;

import org.junit.*;
import static org.junit.Assert.*;

import org.apache.commons.logging.*;
import org.apache.log4j.*;

public class Test01
{
    @Test
    public void homePage() throws Exception
    {
        LogFactory.getFactory().setAttribute("com.gargoylesoftware.htmlunit", "org.apache.commons.logging.impl.Log4JLogger");
        LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Log4JLogger");


        org.apache.log4j.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.WARN);
        org.apache.log4j.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.WARN); 

        Logger.getRootLogger().setLevel(Level.DEBUG);


        Logger.getRootLogger().debug("Start");

        WebClient webClient = new WebClient()

        HtmlPage page = webClient.getPage("https://google.com");

        Logger.getRootLogger().debug("End");
    }

【问题讨论】:

    标签: log4j htmlunit apache-commons-logging


    【解决方案1】:

    所以在对日志输出进行大量试验和观察之后,我发现了这一点。事实证明,Apache HTTPCLient website 上的 HttpClient 文档不正确(事实上存在断开的链接)。事实证明,HTTPClient 使用的记录器的名称与文档中指定的不符。正确的记录器根名称是“http”而不是“httpclient”,这意味着我执行的所有试验都为零效果。

    我正在使用截至今天(2016 年 11 月 15 日)的 org.apache.httpcomponents.httpclient_4.5.2 和 org.apache.httpcomponents.httpcore_4.4.5。

    这里是一个示例 log4j.properties 文件,可以很好地控制 HTMLClient 日志记录

    # Tell the root logger what appenders and level to use
    log4j.rootLogger=DEBUG, A1, A2
    
    
    # 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 #####
    
    log4j.appender.A1=org.apache.log4j.ConsoleAppender
    log4j.appender.A1.layout=org.apache.log4j.PatternLayout
    log4j.appender.A1.layout.ConversionPattern=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n
    
    
    ##### File Appender #####
    
    log4j.appender.A2=org.apache.log4j.FileAppender
    log4j.appender.A2.File=mylogfile.log
    log4j.appender.A2.layout=org.apache.log4j.PatternLayout
    log4j.appender.A2.layout.ConversionPattern=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n
    log4j.appender.A2.Append=false
    

    【讨论】:

      【解决方案2】:

      对于 log4j,您应该将 log4j.logger.com.gargoylesoftware.htmlunit 设置为 ERROR

      log4j.logger.com.gargoylesoftware.htmlunit=ERROR
      

      第二个选项:在您的代码中,在声明您的 Web 客户端后添加:

      LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
      
      java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF); 
      java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);
      

      【讨论】:

      • 感谢您的帮助。属性文件中的 log4j.logger.com.gargoylesoftware.htmlunit=ERROR 什么都不做。代码 sn-ps 工作,但它关闭了根记录器的调试日志。我试过 SimpleLog 和 Log4JLogger,但没有运气。我无法理解为什么我的 log4j.properties 文件中的设置不能为不同的记录器设置不同的级别。我已经用我正在使用的 log4j.properties 文件更新了我的原始问题。
      • 顺便说一句。我对 Authorize.net 记录 html 线路详细信息有同样的问题。
      • 经过试验,似乎只使用了根记录器。设置 org.apache.commons.logging.Log 和 com.gargoylesoftware.htmlunit 的级别和属性没有效果。唯一有效的是如果我在 log4j.properties 文件中设置一个级别,或者我执行 Logger.getRootLogger().setLevel(Level.WARN)。这两种解决方案都会影响所有日志记录,而不仅仅是我想控制的单个日志记录。这不可能这么难,我错过了什么吗?
      猜你喜欢
      • 1970-01-01
      • 2013-12-10
      • 2017-11-11
      • 2012-10-23
      • 1970-01-01
      • 2014-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多