How to write log messages into a specific file

Please let me know how to write log messages into a specific file similar to log4j messages.
An example will be great

See the following post:

In short, Katalon Studio does not let you change the Logback configuration. So you can not write log messages into a specific file of your choice.

Using the following i am able to wring infomative message on the console .
KeywordLogger logger = new KeywordLogger()
logger.logInfo(“TESTING LOGGER IN KATALON”)
I want the same message to be logged into which is available for later use.
While going through katalon Reports file after the execution of the test suite i get the above message
“TESTING LOGGER IN KATALON” in execution0.log in xml format which is not readable.
Pls guide.

Katalon Team recommends recommends com.kms.katalon.core.util.KeywordUtil rather than com.kms.katalon.core.logging.KeywordLogger.

Let me show you 3 codes as Test Case examples.

TC1:

import com.kms.katalon.core.util.KeywordUtil
KeywordUtil.logInfo("TESTING LOGGER IN KATALON")

This shows how to use KeywordUtil.logInfo(String) method. It has nothing new.


TC2:

import com.kms.katalon.core.util.KeywordUtil

/**
 * Extending KeywordUtil class on the fly.
 * Add a new method "forkInfo(String msg)" which logs messages into a file as you like
 * as well as calling "logInfo(String msg)" internally.
 */
File out = new File('./output2.log')
if (out.exists()) {
	out.delete()
}
KeywordUtil.metaClass.static.forkInfo = { String message ->
	out.append(message)
	delegate.logInfo(message);
}

KeywordUtil.forkInfo("TESTING LOGGER IN KATALON\n")
KeywordUtil.forkInfo("TESTING LOGGER IN KATALON again\n")
KeywordUtil.forkInfo("TESTING LOGGER IN KATALON again and again\n")

TC2 adds a new method forkInfo to the KeywordUtil class on the fly. The forkInfo method writes message into a file of your choice. And the message is logged into the Katalon’s original console.


TC3:

import com.kms.katalon.core.util.KeywordUtil


/**
 * Extending KeywordUtil class on the fly.
 * Override the predefined "logInfo(String msg)" method 
 * so that it logs messages into a file as you like
 * while retaining its original behavior
 * 
 * You can find the source of the KeywordUtil class at
 * https://github.com/katalon-studio/katalon-studio-testing-framework/blob/master/Include/scripts/groovy/com/kms/katalon/core/util/KeywordUtil.java
 */
File out = new File('./output3.log')
if (out.exists()) {
	out.delete()
}
KeywordUtil.metaClass.static.logInfo = { String message ->
	out.append(message)
	delegate.logger.logInfo(message)
}

KeywordUtil.logInfo("TESTING LOGGER IN KATALON\n")
KeywordUtil.logInfo("TESTING LOGGER IN KATALON again\n")
KeywordUtil.logInfo("TESTING LOGGER IN KATALON again and again\n")

TC3 changes the behaviour of the KeywordUtil.logInfo(String) method on the fly. The method writes messages into a file of your choice as well as it original behaviour of logging into the Katalon’s console.


I have made a GitHub project where you will find the above examples resides.


You should check the contents of the output2.log and output3.log file. I am sure you would be disappointed, because you will find no meta-data is logged. No timestamp, no class names. Without those meta-data, outputX.log files are just useless.

Do you want the meta-data logged? Then you surely want to customise the Logback configuration; and unfortunately Katalon does not allow you to do it.

@devalex88

Any comment?

3 Likes

Curious about metaClass?

You need to go through “Groovy Metaprogramming” course. See

@kazurayam Great post. :clap:

You should make that a Tips article and make a link to it from this thread. Let me know if you want me to split it for you.

Modifying the KeywordUtil class on the fly is an interesting programming exercise, but practically useless because the output2.log has no meta-data logged. I am afraid this would not worth an article.

I thought everyone knew, @devalex88 is not allowed to talk on the forum. He is locked in the dungeon at KMS Towers where they feed him once a day and he is forced to produce 2 new versions of KS a week.

https://s3.envato.com/files/170172205/20_62_16_Hacker_Hood_Working_on_Computer_Floating_Terminal_text.jpg

Better to ask @duyluong @ThanhTo for their opinions.

1 Like

Thanks for the reply.
As rightly pointed by you , i want output with meta data like timestamp,class names etc similar to as generated generated by log4j but katalon doesn’t allow it .