如何使用 Log4cxx 或 log4j 记录进程 ID

发布于 2024-10-04 09:29:52 字数 135 浏览 7 评论 0原文

我正在使用 log4cxx 我的项目,我可以使用 [%t] 标记来记录当前线程 id,如何在其中或 log4j 中记录进程 id?

我正在使用 ConversionPattern &基于 xml 的配置文件。

谢谢,

I am using log4cxx my project and i can able to log current thread id using [%t] marker, how to log process id in it or log4j?.

I am using ConversionPattern & xml based configuration file.

Thanks,

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

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

发布评论

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

评论(5

站稳脚跟 2024-10-11 09:29:52

基于上面的答案,我将在 log4j 中执行此操作,如下所示:

import java.lang.management.*;
import org.apache.log4j.MDC;

private String getPID() {
  RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();
  return rt.getName();
}

private void configLog4j() {
  // call this from somewhere before you start logging
  MDC.put("PID", getPID());
}

然后在我的 log4j.properties 中:

log4j.appender.FILE.layout.ConversionPattern=%d %X{PID} [%t] %p %c{1} %x - %m%n

这实际上会生成一个由 ID 号和主机名组成的 PID,至少在我的 Java 实现上是这样,并且从我读到的内容来看,这可能是特定于实现的。你可以更进一步,只分离出 PID。

Based on the above answers, I'm going to do this in log4j as follows:

import java.lang.management.*;
import org.apache.log4j.MDC;

private String getPID() {
  RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();
  return rt.getName();
}

private void configLog4j() {
  // call this from somewhere before you start logging
  MDC.put("PID", getPID());
}

Then in my log4j.properties:

log4j.appender.FILE.layout.ConversionPattern=%d %X{PID} [%t] %p %c{1} %x - %m%n

This will actually yield a PID that consists of the ID number and the hostname, at least on my implementation of Java, and from what I read that could be implementation specific. You could go further and split out just the PID.

锦爱 2024-10-11 09:29:52

我已经浏览了 log4j 和 log4cxx 的文档,但没有找到任何有关记录进程 id 的信息。

所以,简而言之:不,你不能记录进程 ID,至少不能直接记录。

由于您使用的是 C++,因此您可以获得程序的 PID。 这里是如何在Linux(此链接中的Ubuntu)中执行此操作。 这里是如何在 Windows 中执行此操作。

在程序启动时获取 PID,使用 MDC 并将您的 PID 放入它。

我不认为有更好的方法。

在 log4j 中执行此操作会更加棘手,因为我知道正在运行的程序无法使用标准 Java 类获取其 PID。

I've grepped through log4j's and log4cxx's documentation and nowhere did I find anything about logging process id.

So, to be short: no, you can't log process id, at least not directly.

Since you're using C++, you can get your program's PID. Here is how to do it in Linux (Ubuntu in this link). Here is how do do it in Windows.

Get that PID at your program start, use an MDC and put your PID in it.

I don't think there's a better way.

Doing this in log4j would be even trickier, since I know of no way for a running program to get it's PID using standard Java classes.

林空鹿饮溪 2024-10-11 09:29:52

这在任何 log4xxx 中都不存在,但只要稍加努力,您就可以自己制作。实际上,如果您不介意编写一些代码,这是一个非常简单的任务。这基本上是我做过几次的事情 - 覆盖实际的附加程序或其布局,确保您的类将进程 ID 粘贴到事件属性映射中。然后按名称使用该属性,就像它是 MDC 属性一样。像上面建议的那样直接使用 MDC 并不是最好的选择,因为它们是线程绑定的,并且您必须确保每个线程在启动时都放置 PID。但是,如果您不能或不想覆盖附加程序或布局,那么这可能是唯一的选择。

This doesnt exist in any of the log4xxx, but with a litle effort you can make it yourself. Actually it's a very simple task if you don't mind a little coding. This is basically what I did few times - override actual appender, or it's layout, make sure your class sticks the process ID into event properties map. Then use this property by name as if it was an MDC property. Using MDC directly like suggested above is not the best choice because they are thread bound and you will have to make sure every thread puts the PID when it starts. But if you can't or don't want to override the appender or layout, then this would probably be the only option.

橙味迷妹 2024-10-11 09:29:52

@skiphoppy 的答案对于 Log4j1.x 非常有效,但我认为可以对其进行更新以显示它在新 Log4j2 中的工作原理。

(注意:我尝试将其作为上述帖子的编辑提交,因为它只是答案代码的一个小修订,但由于我的修订被拒绝,我将其作为单独的答案发布。)

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import org.apache.logging.log4j.ThreadContext;

private String getPID() {
  RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();
  return rt.getName();
}

private void configLog4j() {
  // Here is the Log4j2 way
  ThreadContext.put("PID", rtmx.getName());
}

正如skiphoppy的回答所述,它输出的不仅仅是进程ID。例如,在我的机器(Fedora 20)上:

[电子邮件受保护]

您可以使用以下代码仅提取进程 ID,放置在 XML 配置文件中: %replace{%X{PID}}{[A-Za-z@\.]*}{}

给定上面进程 ID 的输出:

[电子邮件受保护]

正则表达式将生成

16237

The answer by @skiphoppy works very well for Log4j1.x, but I thought it could be updated to show how it works in the new Log4j2.

(NOTE: I tried to submit this as an edit of the posting above as it is only a minor revision of the answer code, but I'm posting it as a separate answer since my revision was rejected.)

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import org.apache.logging.log4j.ThreadContext;

private String getPID() {
  RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();
  return rt.getName();
}

private void configLog4j() {
  // Here is the Log4j2 way
  ThreadContext.put("PID", rtmx.getName());
}

As skiphoppy's answer states, it outputs a little more than just the process ID. For instance, on my machine (Fedora 20):

[email protected]

You can extract just the process id with the following code, placed in your XML configuration file: %replace{%X{PID}}{[A-Za-z@\.]*}{}

Given the output above for the process id:

[email protected]

the regex will produce

16237

时光沙漏 2024-10-11 09:29:52

Log4J 中没有功能可以实现此目的,但是您可以传入进程 ID 并使用它。

这篇博文展示了一种解决方法:http://blackbeanbag.net/wp/2009/01/14/log-file-management-in-coherence-using-log4j/

基本上,将进程 ID 作为系统属性传递,然后在 Log4j 模式中使用它。

显然,这是由于 JVM 没有提供简单的方法来访问进程 ID。从 JDK1.5+ 开始,这个 可能有效。
(从死链接存档 http://www.theresearchkitchen.com/archives/100

There is no feature in Log4J to achieve this, however you could pass the process id in and use that.

This blog post shows one way to go about it: http://blackbeanbag.net/wp/2009/01/14/log-file-management-in-coherence-using-log4j/

Basically, pass in the process id as a system property and then use that in the Log4j pattern.

Apparently, this is due to the JVM not providing an easy method to access the process id. From JDK1.5+, this may work.
(Archived from dead link http://www.theresearchkitchen.com/archives/100 )

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