LocustIO:在写入日志文件时显示日志

LocustIO: display logs while writing to log file

我找到了 create log files on LocustIO 的方法。它成功地将所有日志写入文件中。

有没有办法让终端显示日志并将其写入日志文件?这样我就可以轻松监控结果而无需每次都打开日志文件。

以编程方式尝试但仍不打印。

在 运行 locust -f my_locust_file.py --logfile=locustfile.log

之后不打印任何内容
@task(1)
def fetch_records(self):
... 
    response = self.client.get(full_result, auth=login_creds, headers=headers)
    entry_log = "Fetch Records | Username: {}\tPassword: {} | Response: {}".format(self.username, self.password, response)
    logging.info(entry_log)
    print(entry_log)

更新
也尝试使用 python 记录器和 运行 locust -f my_locust_file.py 而不使用 --logfile。它在控制台中显示但不在日志文件中输出。

def on_start(self):
    logging.basicConfig(filename=my_logfile, level=logging.INFO)

改编自 here 的方式。最好放在class级别,这样就不会遇到文件中的重复日志。

log = logging.getLogger()
log.setLevel(logging.INFO)

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

fh = logging.FileHandler('locustfile.log')
fh.setLevel(logging.INFO)
fh.setFormatter(formatter)
log.addHandler(fh)

log.info("Fetch Records | Username: {}\tPassword: {} | {}".format(self.username, self.password, response))