Reputation: 55
I have a Python script that takes in a log level and sets that to the default level, so once log messages are passed through, it will print or not print according to the level hierarchy (i.e. all messages print if default is DEBUG, only CRITICAL & ERROR messages if default is ERROR).
My Python code is along the lines of this:
# Sets default log level.
def set_default_level(self,level):
levels = {
'DEBUG': logging.DEBUG,
'INFO': logging.INFO,
'WARNING': logging.WARNING,
'ERROR': logging.ERROR,
'CRITICAL': logging.CRITICAL
}
# Sets up configuration for logging message.
logging.basicConfig(format='%(levelname)s: %(message)s', filename = 'log_file.log', filemode = 'w', level=levels[level])
def log_message(self, lvl, message):
msg_print = {
'[DEBUG]': logging.debug,
'[INFO]': logging.info,
'[WARNING]': logging.warning,
'[ERROR]': logging.error,
'[CRITICAL]': logging.critical
}
msg_print[lvl](message)
This code DOES work while running Python alone. However, when I run this code with Robot Framework, it's not creating/writing to the file. The framework I have currently is along the lines of this:
Test Validate Info Prints
[Documentation] Checks if all BUT debug messages are printed.
... Output should not contain any DEBUG level messages.
Set Default Level ${INFO_LVL}
Log Message ${DEBUG} ${DEBUG_MSG}
Log Message ${INFO} ${INFO_MSG}
Log Message ${ERROR} ${ERROR_MSG}
Log Message ${WARNING} ${WARNING_MSG}
Log Message ${CRITICAL} ${CRITICAL_MSG}
${LogFile}= Get File ./log_file.log
Should contain ${LogFile} this build has no associated authentication!
Should contain ${LogFile} S3 Bucket connected successfully
Should contain ${LogFile} No working Internet connection available
Should contain ${LogFile} Application has failed.
Should not contain ${LogFile} please debug this
This is only testing the INFO default level. Other test cases are very similar to this one, but all of them are having the same issue with file creation. I've looked into the issue, but haven't found anything of use. I was using PowerShell at first to run the framework, then I switched to Git BASH and same issue. I am also running on Windows 7.
Thanks in advance!
Upvotes: 2
Views: 1090
Reputation: 1179
Tried with your code! and I have started believing that it is expected behavior.
Why?, because logging in Robot Framework must be handled using the same "Logging" python library that you are using in your code.
So, when you are actually passing your [INFO], [DEBUG] or any other messages, you are in fact not creating a new logger, it would be as good as passing it to the existing logger of the Robot Framework! Hence we see all the messages in the log.html of robot framework. as below:
20161209 12:24:58.497 WARN Application has failed.
20161209 12:24:58.499 WARN No working Internet connection available
20161209 12:24:58.501 WARN this build has no associated authentication!
It is just a thought though!
Upvotes: 1