首页 > 解决方案 > 将 logging.debug 保存在变量中或一一发送

问题描述

我想保存logging数据,就像它在控制台中一一显示一样,在一个变量中(每个异常一个错误,然后用新的变量重写变量),以便我可以将它发送到用户的聊天室(在 Pyrogram 中) 在每个新日志上......像这样:

try:
    func()
except Exception as e:
    errlog =  logging.debug("Error", exc_info=True)
    app.send.message(chat_id=1, text=errlog)

我还通过迭代尝试了上面的代码logging.debug(这表明我对如何处理它一无所知!)...
我只知道我应该编写自己的处理程序,但我不知道该怎么做(发送每个在控制台或本地文件之外的定义级别上逐一记录)。尝试了这个链接(下面的代码),但它没有工作......

except Exception as e:
    logger = logging.getLogger('basic_logger')
    logger.setLevel(logging.DEBUG)
    log_capture_string = io.StringIO()
    ch = logging.StreamHandler(log_capture_string)
    ch.setLevel(logging.DEBUG)
    formatter = logging.Formatter(" %(asctime)s  ⚠ #%(levelname)s   %(message)s",
    datefmt='%Y-%m-%d %H:%M:%S')
    ch.setFormatter(formatter)
    logger.addHandler(ch)
    logger.debug('DEBUG', exc_info=True)
    logger.info('INFO', exc_info=True)
    logger.warn('WARN', exc_info=True)
    logger.error('ERROR', exc_info=True)
    logger.critical('CRITICAL', exc_info=True)
    LogErr = log_capture_string.getvalue()
    app.send_message(chat_id=LogGpID, text=LogErr)

换句话说,我想将所有这些消息作为变量放入控制台上出现的text参数中。 我应该怎么做才能做到这一点?send_message...

标签: pythonloggingerror-handlingpyrogram

解决方案


推荐阅读