python - 将 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
解决方案
推荐阅读
- php - 为什么我的数据没有插入数据库
- java - 将每个 ID 的数据从 firebase 读取到微调器中
- angular - 错误 TS2300:将我的代码移动到 Angular CLI 后标识符重复
- ios - 在 swift 中使用 Alamofire 的 Json 请求失败
- laravel - 如何在不需要使用 Laravel 的情况下传递方法,尝试获取非对象的属性“id”
- c# - 在 c# (^) 符号中使用 Excel 公式
- command-line-interface - 角度飞镖的命令行
- docker - 在 docker run 命令中拉取访问被拒绝
- amazon-web-services - 客户端请求正文被缓冲到一个临时文件
- r - 使用 data.table 的 I 中的反引号引用的列名子集时的奇怪行为