【问题标题】:Using Python Logging module to save logs to S3, how to capture all levels of logs for all modules?使用 Python Logging 模块将日志保存到 S3,如何捕获所有模块的所有级别的日志?
【发布时间】: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 中提到的解决方案,但我有两个问题:

  1. 当我检查 s3 时,输出中不存在任何“前缀”。
  2. 仅显示 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


【解决方案1】:

您至少可以按照以下文档添加关卡名称(或时间):
Changing the format of displayed messages

要获得 DEBUG,您还需要使用以下内容而不是 INFO

logging.basicConfig(..., level=logging.DEBUG)

【讨论】:

    猜你喜欢
    • 2019-12-08
    • 2021-11-24
    • 1970-01-01
    • 2013-03-30
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 2019-11-08
    相关资源
    最近更新 更多