python - 使用 Structlog 获取最后一个日志值并将其作为变量传递给函数
问题描述
目前我正在尝试实现一个函数调用,该函数调用将失败的消息从转换器发送到带有 Kafka 的 DLQ 主题。作为 DLQ 消息的一部分,我想包含我们也记录的异常错误。
编码:
except json.decoder.JSONDecodeError:
log.error('failed to parse json',
topic=msg.topic(),
partition=msg.partition(),
offset=msg.offset()
)
produce_dlq_message(converters.get("DLQ"), msg, error_message)
我需要获取最新 log.error() 调用的值并将其分配给变量:error_message
我在另一个异常块中调用了这个完全相同的函数,但它在 log.error() 调用中具有不同的值,所以我需要一些能够获取最后/最新错误消息的东西。
解决方案
我最终使用了这个解决方案。
try:
...
except json.decoder.JSONDecodeError as e:
log.error("failed to parse json",
topic=msg.topic(),
partition=msg.partition(),
offset=msg.offset(),
)
error_message = str(e)
produce_dlq_message(converters.get("DLQ"), msg, error_message)
推荐阅读
- android - FileNotFoundException 在一个函数中,而另一个函数正在使用该文件而没有问题
- php - AltoRouter 不执行路由,但它在 localhost (LAMP) 中工作
- python - 将python中的CSV文件导入numpy数组
- javascript - 反应中的服务器连接失败/useEffect
- python - 无法在 iframe selenium 中找到元素
- python - 无法优化此代码,想知道为什么它也运行如此缓慢 - 我该如何优化此代码?
- c# - 为什么我的查询无法在 EF 中加载相关数据?
- javascript - 处理点击事件
- pointers - 当 SI 指针被指定指向一个偏移量时,它指向哪里?
- python - 如果行的索引在另一个数据框中不存在,则将数据框中的某些值替换为 NaN