我能想到的唯一方法是编写自己的
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而不是当然的日志……或者也可以。)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)