是否可以拦截对 System.out.print* 和 System.err.print*(在 Java 中)的调用并为它们添加时间戳?别担心,我们使用常用的日志记录框架,但偶尔会出现一些 sys.out 泄漏,最好知道它何时发生,以便我们可以将其绑定(bind)到正确的日志文件。
最佳答案
你可以的。
Reassigns the "standard" output stream. First, if there is a security manager, its checkPermission method is called with a RuntimePermission("setIO") permission to see if it's ok to reassign the "standard" output stream.
public class CustomPrintStream extends PrintStream{
//override print methods here
}
System.setOut(new CustomPrintStream());
关于java - 在 Java 中拦截 System.out 和前置日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17247688/