我有一个 Python 脚本,它接受日志级别并将其设置为默认级别,因此一旦日志消息通过,它将根据级别层次结构打印或不打印(即,如果默认为 DEBUG,则打印所有消息,如果默认为 ERROR,则只有 CRITICAL 和 ERROR 消息)。
我的 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 上运行。
提前致谢!
请您参考如下方法:
尝试了您的代码!我已经开始相信这是预期的行为。
为什么?,因为必须使用您在代码中使用的相同“日志记录”python 库来处理 Robot Framework 中的日志记录。
因此,当您实际传递 [INFO]、[DEBUG] 或任何其他消息时,您实际上并没有创建新的记录器,这与将它传递给 Robot Framework 的现有记录器一样好!因此我们在机器人框架的 log.html 中看到了所有消息。如下:
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!
这只是一个想法!
