首页 > 解决方案 > 使用 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() 调用中具有不同的值,所以我需要一些能够获取最后/最新错误消息的东西。

标签: pythonconfluent-kafka-pythonstructlog

解决方案


我最终使用了这个解决方案。

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)

推荐阅读