用Java截获System.out和前置日期时间 [英] Intercept System.out and prepend date time in Java
问题描述
是否可以拦截对System.out.print *和System.err.print *(在Java中)的调用并为它们加上时间戳?不用担心,我们使用常规的日志记录框架,但是偶尔会有一些sys.out泄漏出去,很高兴知道它何时发生,因此我们可以将其绑定到正确的日志文件.
is it possible to intercept calls to System.out.print* and System.err.print* (in Java) and prepend a time stamp to them? Don't worry, we use the usual logging frameworks, but occasionally some sys.out leaks out and it would be nice to know when it happen so we can tie it up to the proper log files.
推荐答案
您可以做到.
重新分配标准"输出流.首先,如果有安全管理器,则使用RuntimePermission("setIO")权限调用其checkPermission方法,以查看是否可以重新分配标准"输出流.
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 PrinterStream{
//override print methods here
}
System.setOut(new CustomPrintStream());
这篇关于用Java截获System.out和前置日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!