0

目前我正在尝试实现一个函数调用,该函数调用将失败的消息从转换器发送到带有 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() 调用中具有不同的值,所以我需要一些能够获取最后/最新错误消息的东西。

4

2 回答 2

0

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

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)
于 2021-03-03T09:54:40.087 回答
0

虽然可能有一种方法可以通过multistructlog、处理器等来实现,但我建议采用低技术路线:

try:
   ...
except json.decoder.JSONDecodeError:
    em = {
        "event": "failed to parse json",
        "topic": msg.topic(),
        "partition": msg.partition(),
        "offset": msg.offset(),
    }

    log.error(**em)
    produce_dlq_message(converters.get("DLQ"), msg, json.dumps(em))

这意味着要em序列化两次,但我认为总体而言,简单性使其值得。

于 2021-03-03T06:47:26.603 回答