java删除日志_java – Selenium许多日志(如何删除)

我用Firefox 48尝试了Selenium 3.0.1.

我已经尝试过以下代码:

java.util.logging.Logger.getLogger(“com.gargoylesoftware.htmlunit”).setLevel(Level.OFF);

java.util.logging.Logger.getLogger(“org.apache.commons.httpclient”).setLevel(Level.OFF);

java.util.logging.Logger.getLogger(ProtocolHandshake.class.getName()).setLevel(Level.OFF);

但是一旦我在Netbeans下进行常规测试,……日志仍然会出现:

Dec 02, 2016 9:17:53 AM org.openqa.selenium.remote.ProtocolHandshake createSession

INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end

Dec 02, 2016 9:17:57 AM org.openqa.selenium.remote.ProtocolHandshake createSession

INFO: Detected dialect: OSS

解决这个问题的任何线索?

解决方法:

您必须将记录器固定在内存中或设置logging.properties配置文件.来自java.util.logging.Logger文档:

Logger objects may be obtained by calls on one of the getLogger factory methods. These will either create a new Logger or return a suitable existing Logger. It is important to note that the Logger returned by one of the getLogger factory methods may be garbage collected at any time if a strong reference to the Logger is not kept.

返回新记录器时,日志级别由LogManager确定,LogManager默认使用logging.properties文件中的设置.在您的示例中,可以看到以下内容:

>调用getLogger创建一个新的记录器并从LogManager设置级别.

>您的代码将记录器级别设置为OFF.

> G.C.运行并销毁您的记录器以及刚刚应用的设置.

> Selenium调用getLogger并创建一个新的记录器并从LogManager设置级别.

以下是一个示例测试用例来证明这一点:

public static void main(String[] args) {

String name = "com.gargoylesoftware.htmlunit";

for (int i = 0; i < 5; i++) {

System.out.println(Logger.getLogger(name).getLevel());

Logger.getLogger(name).setLevel(Level.OFF);

System.runFinalization();

System.gc();

System.runFinalization();

Thread.yield();

}

}

这将输出null而不是OFF.

如果您通过持有强引用来固定记录器,则步骤#3永远不会发生,而Selenium应该找到您创建的记录器,其级别设置为OFF.

private static final Logger[] pin;

static {

pin = new Logger[]{

Logger.getLogger("com.gargoylesoftware.htmlunit"),

Logger.getLogger("org.apache.commons.httpclient"),

Logger.getLogger("org.openqa.selenium.remote.ProtocolHandshake")

};

for (Logger l : pin) {

l.setLevel(Level.OFF);

}

}

标签:java,unit-testing,logging,firefox,selenium

来源: https://codeday.me/bug/20190828/1754906.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值