在多个组件中记录全局 ID

发布于 2024-08-11 07:06:35 字数 271 浏览 6 评论 0原文

我有一个系统,其中包含使用 JMS 和 Spring Integration 连接在一起的多个应用程序。消息沿着一系列应用程序发送。

[应用程序A]-> [应用程序B]-> [应用程序 C]

我们在消息头中设置了一个全局 ID,以便我们可以通过系统跟踪每个消息的生命周期。

我希望能够在系统中的任何日志消息前添加消息全局 ID。

还有其他人这样做过吗?有什么方法可以将此变量关联到线程,以便我可以在将来的方法中访问它吗?我不想在系统的方法中传递变量。

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications.

[App A] -> [App B] -> [App C]

We set a global id in the message header so we can trace each message lifecycle through the system.

I would like to be able to prepend any log message in the system with the message global id.

Has anyone else done this? Is there any way to associate this variable to the Thread so I can access it in future methods? I'd rather not pass the variable around in methods of the system.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终难愈 2024-08-18 07:06:35

我认为 ThreadLocal 可能就是您想要的,尽管有些人可能会发现这种方法滥用了 ThreadLocal 的目的或良好的设计。类似于:

public class MyIDManager {
  public static final ThreadLocal<Long> myID = new ThreadLocal<Long>();
}

...
// set ID at some point
MyIDManager.myID.set(theNewID);

...
// read it later
long currentID = MyIDManager.get();

这里的神奇之处在于 myID 的值实际上是特定于线程的,并且从不同线程访问时会有所不同。

然后,您可以使用 ID 执行您喜欢的操作,包括记录它。

I think ThreadLocal may be what you want here, though some may find this approach an abuse of ThreadLocal's purpose, or good design. Something like:

public class MyIDManager {
  public static final ThreadLocal<Long> myID = new ThreadLocal<Long>();
}

...
// set ID at some point
MyIDManager.myID.set(theNewID);

...
// read it later
long currentID = MyIDManager.get();

The magic here is that myID's value is actually specific to a Thread, and will be different when accessed from different threads.

You can then do what you like with the ID, including logging it.

打小就很酷 2024-08-18 07:06:35
Thread t = Thread.currentThread();
t.setName("Your ID Here");
Thread t = Thread.currentThread();
t.setName("Your ID Here");
撧情箌佬 2024-08-18 07:06:35

我相信第一篇文章提到的另一个答案是简单地要求您的日志记录框架在日志语句中包含线程名称。例如,log4j 允许您在其 PatternLayout 中添加带有“t”的线程名称: http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html 我也在其他框架中看到了这一点。

Another answer, which I believe the first post is alluding to, is to simply ask your logging framework to include the thread name in the log statement. For example, log4j lets you add the thread name with 't' in its PatternLayout: http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html I've seen this in other frameworks too.

酒浓于脸红 2024-08-18 07:06:35

顺便说一句,这种方法是更广泛的企业日志记录模式的一部分。我试图在此处记录此模式。总结是:

TL;博士

在“进程”最早开始时创建一个 ID,它是
“原始 ID”和“唯一 ID”的组合。这个非商业
相关的全局初始 ID (GIID),应该用于每个日志
输出以提​​供机器可读性和工具使用。当每一个新
获取或生成业务级别或操作id,
记录以提供与此 GIID 的“关联”。这类似于
各种现有的实践,因此它被呈现为软件设计
模式。

BTW, this approach is part of a wider enterprise logging pattern. I attempted to document this pattern here. The summary is:

TL;DR

At the earliest inception of a ‘process’ create an ID which is a
combination of an ‘origin id’ and ‘unique id’. This non-business
related global inception ID (GIID), should be used for every log
output to provide machine readability and tool use. When each new
business level or operational id is obtained or generated, it is
logged to provide an ‘association’ with this GIID. This is similar to
various existing practices so it is presented as a Software Design
Pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文