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.
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.
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
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”
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.
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:
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.