在Spring中跨线程传递Log4j2 ThreadContext

时间:2021-06-22 21:51:09

Goal is to provide unique Id to all the log statements per request (which can spawn multiple threads due to AsynRestTemplate usage). I have a situation where "ListenableFutureCallback" onFailure() is retuning an exception. When the exception is thrown, Global Exception Handler is written extending "ResponseEntityExceptionHandler". I need to pass Log4j2 ThreadContext from onFailure to ResponseEntityExceptionHandler. someErrorHandler() method, so that log4j can log unique id for that request to correlate what happened to that call.

目标是为每个请求的所有日志语句提供唯一的Id(由于AsynRestTemplate用法,它可以生成多个线程)。我有一种情况,其中“ListenableFutureCallback”onFailure()重新调整异常。抛出异常时,将编写Global Exception Handler扩展“ResponseEntityExceptionHandler”。我需要将log4j2 ThreadContext从onFailure传递给ResponseEntityExceptionHandler。 someErrorHandler()方法,以便log4j可以记录该请求的唯一ID,以关联该调用发生的事情。

I can provide more details in case required. Appreciate your help.

如果需要,我可以提供更多细节。感谢您的帮助。

1 个解决方案

#1


0  

This is only a guess, but I would see if you can add the ThreadContext attributes as headers to the response. I would think they would be available in all the places you want them.

这只是一个猜测,但我会看看你是否可以将ThreadContext属性作为标题添加到响应中。我认为它们可以在你想要的所有地方使用。

#1


0  

This is only a guess, but I would see if you can add the ThreadContext attributes as headers to the response. I would think they would be available in all the places you want them.

这只是一个猜测,但我会看看你是否可以将ThreadContext属性作为标题添加到响应中。我认为它们可以在你想要的所有地方使用。