How can I change the message format of WebUI.comment("msg")? + logging into FILE

I found that <my Katalon Home>/plugins/com.kms.katalon.core_1.0.0.201901020244.jar file contains /resources.logback/logback.xml and /resources.logback/logback-console.xml.

It is likely that the xml files are loaded from the jar file via CLASSPATH and used by Katalon Studio runtime. Therefore editing <my Katalon home>/configuration/resources/logback/logback.xml does not have any effect.

1 Like

OK, I will try modifying the logback configuration object runtime by Groovy metaprogramming.

1 Like

I haven’t tried yet but will this question help?

I have made a demo project on Github:

Solution

Manually editting logback.xml file is not a good idea. I should modify logback LoggerFactory object runtime. I should be able to do it using Groovy’s metaprogramming feature.

Description

I made a custom keyword com.kazurayam.ksbackyard.CustomLoggerFactory. I would not explain this code much. It performs a surgery for LogBack LoggerFactory object using Groovy’s ExpandoMetaClass.

I made a test case CustomizeLogger4CommentKeywordDemo, that is:

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

// make a patch to the Logback LoggerFactory object runtime
CustomKeywords.'com.kazurayam.ksbackyard.CustomLoggerFactory.customizeLogger4CommentKeyword'()

def arg0 = 2
def arg1 = 3
def result = arg0 + arg1
WebUI.comment("arg0 + arg1 = ${result}")

When I ran the test case, in the Console I see the following messages:

2019-01-11 14:31:12.826 INFO c.k.katalon.core.main.TestCaseExecutor   - --------------------
2019-01-11 14:31:12.826 INFO c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/CustomizeLogger4CommentKeywordDemo
2019-01-11 14:31:13.579 INFO k.k.c.m.CustomKeywordDelegatingMetaClass - com.kazurayam.ksbackyard.CustomLoggerFactory.customizeLogger4CommentKeyword is PASSED
arg0 + arg1 = 5
2019-01-11 14:31:13.774 INFO c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/CustomizeLogger4CommentKeywordDemo

I like this plain output:

arg0 + arg1 = 5

I have got a success.

How to reuse it for you

You can copy the keyword com.kazurayam.ksbackyard.CustomLoggerFactory
and paste it into your Katalon Studio project.

And in your test case, call CustomKeywords.'com.kazurayam.ksbackyard.CustomLoggerFactory.customizeLogger4CommentKeyword'().

Then WebUI.comment() will become brief to the point.

1 Like

Feature Request:

Katalon Studio should load <my Katalon home>/configuration/resources/logback/logback.xml from disk rather than loading the config info from the jar file via CLASSPATH.

Then I would be able to manually edit the config file. No tricky coding would be required.

Or, are you afraid of a risk that a stupid user may destroy the logback.xml file so that Katalon Studio become unable to work at all? — well, I think you may say “do it at your own risk”

2 Likes

I think a possible improvement here is that Katalon Studio will look for the Logback configuration file in the current project first.

2 Likes

I like this idea.

1 Like

@devalex88 yeah … will be great if more log properties, not only log level, can be overridden with the project config

@devalex88

Have you added this issue to the Katalon’s list of backlogs? If not yet, please do.

1 Like

I extended my demo project on Github:

New version of the demo shows how to enable logging into FILE in Katalon Studio.

An example log to FILE is this:

2 Likes

Let me say this again.

Have you added this issue to the Katalon’s list of backlogs? If not yet, please do.

2 Likes

I think this thread is worth necro-ing. Are there any plans for a project level logback configuration file?

I tried to customize WebUI.comment(String message) so that it write messages into a file. But it did not work.

I made the “develop” branch in my project and tried it.

In the develop branch, “Test Cases/Example_customizeCommentKeyword4msgOnly2Console” seems working, but “Test Cases/Example_customizeCommentKeyword4levelAndMsg2File” does not work as I expected. I expected to find a line of “arg0 + arg1 = 5” in the ./comment.log file but the file remains empty.

Reason? — I do not see. I guess that Katalon Studio creates and cache an instance of com.kms.katalon.core.keyword.builtin.CommentKeyword during certiain timing of test execution. Once the instance is cached in an instance of JVM, it is too late if my test case script calls my com.kazurayam.ksbackyard.CustomeLoggerFactory keyword with intension to change the behaviour of org.slf4j.LoggerFactory. And I do not see when KS creates and cache the instance. I can not intervene that process.


Conclusion:

com.kazurayam.ksbackyard.CustomeLoggerFactory is fragile. You should not use it.

If I want to make a log into file, I would not call WebUI.comment(); I would just create a custom class that writes messages into file; as simple as this.

1 Like

@kazurayam did speak thusly…


:nerd_face:

:sunglasses:

Thank you for your interest.

My Custom Keyword sometimes works, but sometimes doesn’t. It is not stable. :sob:

As you know, I mess around with reports and WebUI.comment a lot. I’ll play with this and report back.

Do you think it’s possible that “something” is messing with your expando? Maybe resetting it?

I read the source code published at https://github.com/katalon-studio/katalon-studio-testing-framework/blob/master/Include/scripts/groovy/

I started reading com.kms.katalon.core.keyword.builtin.CommentKeyword and trace the KS internal flow.

I found com.kms.katalon.core.keyword.internal.KeywordExecutor maintains a cache of instances of buillt-in keyword, which would include WebUI.comment() of course. Once an instance is cached, it is too late even if my keyword modifies the factory class.

The source codes are bulky. I gave up finding out at which stage of test execution the com.kms.katalon.core.keyword.builtin.CommentKeyword class is instantiated when I executed my testcases:

both are in the develop branch, not the master branch of GitHub repository.

I was more interested in getting the section I highlighted above to work - not so much interested in writing to a file.

I’m also trying to call the methods directly/statically, without all that 'very.long.string.name.method' stuff. Is that possible? It didn’t work for me. All I got was an empty comment.log.

I am not sure what you mean.

CustomKeywords.'com.kazurayam.ksbackyard.CustomLoggerFactory.customizeCommentKeyword4MsgOnly2Console'()

is equivalent to

import com.kazurayam.ksbackyard.CustomLoggerFactory as CLF

CLF.customizeCommentKeyword4MsgOnly2Console()

Is it what you mean?