硒许多日志(如何删除) [英] Selenium many Logs (How to remove)
问题描述
我尝试了使用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 下运行常规测试,...日志仍然出现:
十二月02,2016 9:17:53 org.openqa.selenium.remote.ProtocolHandshake createSession
INFO:尝试双方言会话,假设Postel的定律在远程终端
Dec 02 ,2016 9:17:57 org.openqa.selenium.remote.ProtocolHandshake createSession
INFO:检测到的方言:OSS
解决这个问题的任何线索
您必须将记录器固定在内存中, code> logging.properties 配置文件。从 java.util.logging.Logger docs:
记录器对象可以通过getLogger工厂方法之一的调用获得。这些将创建一个新的记录器或返回一个合适的现有记录器。需要注意的是,如果Logger的强引用不被保存,任何时候getLogger工厂方法返回的Logger都可能被垃圾回收。
当返回一个新的记录器时,日志级别由LogManager决定,LogManager默认使用来自 logging.properties
文件的设置。在你的例子中,可以看到以下内容:
- 调用getLogger将创建一个新的记录器并从LogManager中设置级别。 >
- 您的代码将记录器级别设置为OFF。
- GC运行并销毁您的记录器以及您刚刚应用的设置。
- 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();
$ p $ null
代替 OFF
。
如果您通过保持强引用来固定记录器, #3从来没有发生,硒应该找到你创建的记录器,其级别设置为关闭。
private static final Logger [ ]引脚;
static {
pin = new Logger [] {
Logger.getLogger(com.gargoylesoftware.htmlunit),
Logger.getLogger(org.apache.commons.httpclient ),
Logger.getLogger(org.openqa.selenium.remote.ProtocolHandshake)
}; (Logger l:pin){
l.setLevel(Level.OFF);
}
}
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);
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?
解决方案 You have to pin your loggers in memory or setup a logging.properties
config file. From the java.util.logging.Logger docs:
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.
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:
- Call to getLogger creates a new logger and sets level from LogManager.
- Your code sets the logger level to OFF.
- G.C. runs and destroys your logger along with settings you just applied.
- Selenium calls getLogger and creates a new logger and sets level from 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();
}
}
Which will output null
instead of 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);
}
}
这篇关于硒许多日志(如何删除)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!