我能想到的唯一方法是编写自己的PrintStream
实现,该实现在调用println
方法时创建堆栈跟踪,以便计算出类名。这将是非常可怕的,但它应该工作...的概念的示例代码证明:(。在你的代码将使得其记录到的log4j而不是当然...或者可能还有)
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");
}
}
如果我删除了实现中的锁定行为以提高应用程序的性能,那么会有什么影响? – 2017-04-29 06:49:00