小编Chu*_*nga的帖子

使用 Loguru 日志库记录导入文件中发出的请求

因此,我在 python 文件(main.py)中编写了程序,该文件使用 api 包装文件(bluerev.py)中的类。我想使用 main.py 中的 loguru 记录器来收集程序中的所有异常 + api 包装器中发出的所有请求。因此,bluerev.py api 包装器中设置的日志记录如下所示:

import logging

#Logging setup
logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())

class BluerevApiRequestHandler:

def __init__(self):
    self.date_str_format = "%Y-%m-%dT%H:%M:%S.%f"

@staticmethod
def setup_logger_for_requests():
    """
    Sets up the requests library to log api requests
    """
    logger.setLevel(logging.DEBUG)
    requests_log = logging.getLogger("requests.packages.urllib3")
    requests_log.setLevel(logging.DEBUG)
    requests_log.propagate = True
Run Code Online (Sandbox Code Playgroud)

main.py 日志记录代码如下所示:

from blurev import *
from loguru import logger
#more imports and code

@logger.catch
def main():
    # associated file and credential locations
    gmail_creds_file = "gmail_creds.json"
    revu_creds_file = r"revu_credentials.json" …
Run Code Online (Sandbox Code Playgroud)

python logging python-3.x python-requests loguru

0
推荐指数
1
解决办法
5415
查看次数

标签 统计

logging ×1

loguru ×1

python ×1

python-3.x ×1

python-requests ×1