Selenium many Logs (How to remove)
Asked Answered
B

1

4

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?

Brawny answered 2/12, 2016 at 2:25 Comment(2)
This is a problem why?Operose
when the logs keeps coming,,, and another instance are making the same call of the code .... it's taking memory for sure... and in a long run it would be a memoy leak, wouldn't @Operose ?Brawny
A
6

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:

  1. Call to getLogger creates a new logger and sets level from LogManager.
  2. Your code sets the logger level to OFF.
  3. G.C. runs and destroys your logger along with settings you just applied.
  4. 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);
    }
}
Adolescence answered 2/12, 2016 at 14:26 Comment(2)
what the difference? i mean, i also set the logger to its' OFF level on the above code....? @jmehreris, I just saw the static variable as the differentBrawny
@Brawny The static reference is a hard reference prevents garbage collection of the logger. If point #3 is allowed to happen the it is as if you never set the level to OFF.Adolescence

© 2022 - 2024 — McMap. All rights reserved.