Turn logging off and on using code

Is there a way to turn logging on and off in a script using code?
I often check for web elements existing in my code, using the verifyElementPresent method (and the FailureHandling.OPTIONAL parameter). What I hate about this is that if the element is not present I get a big old red block of stack trace in the log and console. After 3 years of living with this, today I snapped and looked for an alternative.
I figured it should be easy to create a wrapper method which turns off logging, calls verifyElementPresent and then turns logging back on again… but for the life of me I can’t figure out how to do this, and I’ve been Googling and ChatGPT’ing it for the past three hours! Can someone please help?

UPDATE: I just discovered that the waitForElementPresent method can be used instead, which although still returns a red warning in my logs, at least it’s just one line and not a stack trace. For this reason, I’d still like to be able to disable logging before and after if possible.

1 Like

There isn’t any built-in feature to do this dynamically.


If I were you, I will write a Groovy class like this:

package my;

import internal.GlobalVariable
import com.kms.katalon.core.model.FailureHandling

class LoggingSwitch {
    static FailureHandling turn(FailureHandling original) {
        if (GlobalVariable.LOGGING_OFF != null && GlobalVariable.LOGGING_OFF) {
            return FailureHandling.OPTIONAL
        } else {
            return original
        }
    }
}

You test case would use this as

import static my.LoggingSwitch.turn

WebUI.verifyElementPresent(testobject, 10, turn(FailureHandling.STOP_ONF_FAILURE))

If you specify true to GlobalVariable.LOGGING_OFF, then the turn() will return FailureHandling.OPTIONAL; you can switch the flow control dynamically.

Thank you so much for the suggestion, but if I’m understanding the code correctly this would still result in a red warning/trace in the results? I’m trying to avoid that, so I was either looking for a way to temporarily ‘mute’ logging while I called verifyElementPresent, or (and I know this doesn’t exist) a FailureHandling type such as “IGNORE” which would cause verifyElementPresent not to output a trace to the logs. Does that make sense?

1 Like

OK. I would tell you how to achieve it.

Here you can read the source of WebUI.verifyElementPresent keyword.

there you can find the following code:

            } catch (WebElementNotFoundException ex) {
                WebUIKeywordMain.stepFailed(ExceptionsUtil.getMessageForThrowable(ex), flowControl, null, true)

Taking this as the start, you can trace the call chain, and you will reach to

Line#30

            case FailureHandling.OPTIONAL:
                logger.logWarning(failedMessage, attributes, t);
                break;

You want to remove the Line#31

                    logger.logWarning(failedMessage, attributes, t);

In other words, you want to replace the implementation of the stepFailed method of the KeywordMain class to the new one you created. The new implementation of stepFailed will be almost the same as the original but it has no logger.logWarning(failedMessage, attributes, t); statement.

Then the FailureHandling.OPTIONAL will no longer print a warning message at all.

Now you want to modify the implementation of the com.kms.katalon.core.keyword.internal.KeywordMain.groovy dynamically while your test runs. Is it possible?

Yes. The KeywordMain class is coded in Grovvy. So you can replace the implementation using the “Groovy Metaprogramming” technique.

What is the “Groovy Metaprogramming”? Read the Groovy doc:

1.9.4. ExpandoMetaClass

Groovy comes with a special MetaClass the so-called ExpandoMetaClass. It is special in that it allows for dynamically adding or changing methods, constructors, properties and even static methods by using a neat closure syntax.

How to utilize it in Katalon Studio? Any example? ---- See the following old post of mine.

Good luck!

Thanks! I’ll have a play with this solution - most appreciated.