【发布时间】:2021-09-02 08:40:26
【问题描述】:
我有以下Python 代码extraction.py 的一部分,其中我有很多print 语句,还有从REST API 生成的其他日志。我想创建带有日期和时间前缀的日志文件,例如 log_yyyy-MM-dd-hhminss 以跟踪我的 Python 代码中的所有日志。这将有助于跟踪我的 Python 代码中的每日日志。
import requests
import json
import shutil
import time
import gzip
import os
extraction request:
if status_code == 202:
requestUrl = r2.headers["location"]
print('Extraction is not complete, we shall poll the location URL:')
print(str(requestUrl))
requestHeaders = {
"Prefer": "respond-async",
"Content-Type": "application/json",
"Authorization": "token " + token
}
while (status_code == 202):
print('As we received a 202, we wait 30 seconds, then poll again (until we receive a 200)')
time.sleep(30)
r3 = requests.get(requestUrl, headers=requestHeaders)
status_code = r3.status_code
print('HTTP status of the response: ' + str(status_code))
【问题讨论】:
-
谢谢我刚刚检查过,但我们如何创建日志文件并在最后跟踪此日志文件中的所有日志,文档中没有提到?
-
你在 logging.basicConfig 中定义
-
此链接是否有更好的帮助? docs.python.org/3/howto/logging.html#logging-to-a-file您需要学习如何使用文档,而不是在 SO 上提出这样的通用问题。
-
是的,我会检查这个链接
标签: python logging python-3.7 logfile