【发布时间】:2022-05-02 21:57:43
【问题描述】:
这是我第一次在 Python(3.7) 中使用日志记录模块。我的代码使用导入的模块,这些模块也有自己的日志语句。当我第一次在我的代码中添加日志语句时,我没有使用 getLogger()。我只是使用logging.basicConfig(filename) 并直接调用logger.debug() 来记录语句。当我这样做时,我的脚本和所有导入的模块中的所有日志都会一起输出到同一个文件中。
现在我需要转换我的代码以将日志保存到 s3 而不是文件。我尝试了How Can I Write Logs Directly to AWS S3 from Memory Without First Writing to stdout? (Python, boto3) - Stack Overflow 中提到的解决方案,但我有两个问题:
- 当我检查 s3 时,输出中不存在任何“前缀”。
- 仅显示 INFO 语句。我的印象是
logging.basicConfig(level=logging.INFO)会使其输出所有日志级别为INFO 或 级别,但我只看到INFO。此外,在所有级别之前,只有 INFO 日志会打印到标准输出。我不知道为什么缺少“前缀”。
from psaw import PushshiftAPI
api = PushshiftAPI()
import time
import logging
import boto3
import io
import atexit
def write_logs(body, bucket, key):
s3 = boto3.client("s3")
s3.put_object(Body=body.getvalue(), Bucket=bucket, Key=key)
logging.basicConfig(level=logging.INFO)
log = logging.getLogger()
log_stringio = io.StringIO()
handler = logging.StreamHandler(log_stringio)
log.addHandler(handler)
def collectRange(sub,start,end):
atexit.register(write_logs, body=log_stringio, bucket="<...>", key=f'{sub}/log.txt')
s3 = boto3.resource('s3')
object = s3.Object('<...>', f'{sub}/{sub}@{start}-{end}.csv')
now = time.time()
logging.info(f'Start Time:{now}')
logging.debug('First request')
gen = api.search_comments(after=start, before=end,<...>, subreddit=sub)
r=next(gen)
<...>
quit()
输出:
Found credentials in shared credentials file: ~/.aws/credentials
Start Time:1591310443.7060978
https://api.pushshift.io/reddit/comment/search?<...>
https://api.pushshift.io/reddit/comment/search?<...>
期望的输出:
INFO:botocore.credentials:Found credentials in shared credentials file: ~/.aws/credentials
INFO:root:Start Time:1591310443.7060978
DEBUG:root:First request
INFO:psaw.PushshiftAPI:https://api.pushshift.io/reddit/comment/search?<...>
DEBUG:psaw.PushshiftAPI:<whatever is usually here>
DEBUG:psaw.PushshiftAPI:<whatever is usually here>
INFO:psaw.PushshiftAPI:https://api.pushshift.io/reddit/comment/search?<...>
DEBUG:psaw.PushshiftAPI:<whatever is usually here>
DEBUG:psaw.PushshiftAPI:<whatever is usually here>
感谢任何帮助。谢谢。
【问题讨论】:
-
遇到类似情况,未捕获来自其他导入模型的日志并且缺少具有日志级别的时间戳。你能找到解决办法吗?谢谢!
标签: python python-3.x amazon-web-services amazon-s3 logging