本文介绍了Selenium 许多日志(如何删除)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Firefox 48 上尝试了 Selenium 3.0.1.

I tried the Selenium 3.0.1 with Firefox 48.

而且我已经尝试了以下代码:

And I already tried the following code:

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 下运行通常的测试,...日志仍然出来:

but once i run the usual testing under Netbeans,... the logs still comes out:

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

有解决这个问题的线索吗?

Any clue for solving this?

推荐答案

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

You have to pin your loggers in memory or setup a logging.properties config file. From the java.util.logging.Logger docs:

Logger 对象可以通过调用 getLogger 工厂方法之一来获得.这些将创建一个新的 Logger 或返回一个合适的现有 Logger.需要注意的是,如果没有保留对 Logger 的强引用,由 getLogger 工厂方法之一返回的 Logger 可能会随时被垃圾回收.

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

When a new logger is returned the log level is determined by the LogManager which by default uses settings from a logging.properties file. In your example, it is possible to see the following:

  1. 调用 getLogger 会创建一个新的记录器并从 LogManager 设置级别.
  2. 您的代码将记录器级别设置为关闭.
  3. G.C.运行并销毁您的记录器以及您刚刚应用的设置.
  4. Selenium 调用 getLogger 并创建一个新的记录器并从 LogManager 设置级别.

这里有一个示例测试用例来证明这一点:

Here is an example test case to prove the point:

    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.

Which will output null instead of OFF.

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

If you pin your logger by holding a strong reference then step #3 never happens and Selenium should instead find the logger you created which has the level set to 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);
    }
}

这篇关于Selenium 许多日志(如何删除)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 04:45