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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5712764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:25:42  来源:igfitidea点击:

Redirect System.out.println to Log4J, while keeping class name information

javalog4jslf4japache-commons-logging

提问by David Parks

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.

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

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

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

回答by Jon Skeet

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

我能想到的唯一方法是编写自己的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");
  }
}

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

(在您的代码中,您将使其登录到 log4j 而不是当然......或者也可能如此。)

回答by Ralph

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).

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