0

执行从 Robot Framework 中进行,其中Test.py已作为库导入并testLog()正在执行,而后者又导入Logger.py并调用LogMessage().

测试.py

import Logger
def testLog():
  Logger.LogMessage("This is the first line of the log file.")
  Logger.LogMessage("This is the second line of the log file.")
  Logger.LogMessage("This is the third line of the log file.")

记录器.py

import logging  
def LogMessage(message):
  LOG_FILENAME = "C://Log_Details".log"
  logger = logging.getLogger()    
  logFileHandler = logging.FileHandler(LOG_FILENAME)
  logger.addHandler(logFileHandler)

Log_Details.log

This is the first line of the log file.
This is the second line of the log file.
This is the second line of the log file.
This is the third line of the log file.
This is the third line of the log file.
This is the third line of the log file.  

RIDE 中的消息日志部分在执行期间只记录每行一次,但名为的文件Log_details.log会多次打印它们,即第一行记录一次,第二行记录两次,依此类推。

4

1 回答 1

1

您会收到 1x 消息 1、2x 消息 2 和 3x 消息 3。那是因为您将日志设置作为LogMessage函数的一部分执行,并且每次记录消息时都会在其中添加一个文件日志处理程序......所以在第一次运行之后有 1 个处理程序记录您的消息一次,在第二次调用后,您有 2 个处理程序记录您的消息两次,依此类推...

为了避免这种情况,您只想配置一次记录器。只需将您的日志配置移动到您将在启动脚本时调用一次的函数,然后您就可以使用:

import logging
log = logging.getLogger(__name__)
log.info('smth')

每当您想登录应用程序中的任何其他文件时。

于 2017-09-28T16:34:14.750 回答