我有一个Python脚本,它接受日志级别并将其设置为默认级别,因此一旦日志消息通过,它将根据级别层次结构打印或不打印(即,如果默认为DEBUG,则打印所有消息,仅打印CRITICAL & 错误消息(如果默认为错误)。
我的Python代码是这样的:
# 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)
这段代码在单独运行 Python 时确实可以工作。但是,当我使用 Robot Framework 运行此代码时,它不会创建/写入文件。我目前的框架是这样的:
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
这只是测试 INFO 默认级别。其他测试用例与此测试用例非常相似,但它们都在文件创建方面存在相同的问题。我已经研究过这个问题,但没有发现任何有用的东西。我一开始使用 PowerShell 来运行框架,然后我切换到 Git BASH 并出现同样的问题。我也在 Windows 7 上运行。
提前致谢!