将 System.out.println 重定向到 Log4J,同时保留类名信息

发布于 2024-11-02 08:24:29 字数 453 浏览 2 评论 0原文

我有一些库正在调用 System.out.println,我想通过 log4j 或 commons 日志记录重定向它们。但我特别想保留完全限定的类名,这样我就知道哪个组件生成了日志。

有没有一种好的、有序的方法来完成这个任务?


更新:完成此操作后,我在此处发布了代码:
http://www.bukisa.com/articles/487009_java-how-to-redirect-stderr-and-stdout-to-commons-logging-with-the-calling-class

I have some libraries that are calling System.out.println on me, I'd like to redirect them through log4j or commons logging. But in particular I'd like to keep the fully-qualified-classname so I know what component generated the logs.

Is there a nice, orderly way to accomplish this?


UPDATE: After accomplishing this I posted the code here:

http://www.bukisa.com/articles/487009_java-how-to-redirect-stderr-and-stdout-to-commons-logging-with-the-calling-class

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

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

发布评论

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

评论(2

何必那么矫情 2024-11-09 08:24:29

我能想到的唯一方法是编写自己的 PrintStream 实现,该实现在调用 println 方法时创建堆栈跟踪,以便计算出类名。这将是非常可怕的,但它应该可以工作...概念验证示例代码:(

import java.io.*;

class TracingPrintStream extends PrintStream {
  public TracingPrintStream(PrintStream original) {
    super(original);
  }

  // You'd want to override other methods too, of course.
  @Override
  public void println(String line) {
    StackTraceElement[] stack = Thread.currentThread().getStackTrace();
    // Element 0 is getStackTrace
    // Element 1 is println
    // Element 2 is the caller
    StackTraceElement caller = stack[2];
    super.println(caller.getClassName() + ": " + line);
  }
}

public class Test {
  public static void main(String[] args) throws Exception {
    System.setOut(new TracingPrintStream(System.out));
    System.out.println("Sample line");
  }
}

在您的代码中,您可以让它记录到 log4j 而不是当然...或者也可能。)

The only way I can think of would be to write your own PrintStream implementation which created a stack trace when the println method was called, in order to work out the class name. It would be pretty horrible, but it should work... proof of concept sample code:

import java.io.*;

class TracingPrintStream extends PrintStream {
  public TracingPrintStream(PrintStream original) {
    super(original);
  }

  // You'd want to override other methods too, of course.
  @Override
  public void println(String line) {
    StackTraceElement[] stack = Thread.currentThread().getStackTrace();
    // Element 0 is getStackTrace
    // Element 1 is println
    // Element 2 is the caller
    StackTraceElement caller = stack[2];
    super.println(caller.getClassName() + ": " + line);
  }
}

public class Test {
  public static void main(String[] args) throws Exception {
    System.setOut(new TracingPrintStream(System.out));
    System.out.println("Sample line");
  }
}

(In your code you would make it log to log4j instead of course... or possibly as well.)

俯瞰星空 2024-11-09 08:24:29

如果您可以修改源代码,请查看 Eclipse 插件 Log4E。它提供了一个将 System.out.println 转换为记录器语句(以及许多其他处理日志记录的很酷的东西)的函数。

If you can modify the source code, then have a look at the Eclipse Plugin Log4E. It provides a function to convert System.out.println into logger statements (and many other cool stuff dealing with logging).

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