钩入System.out.println();并修改 [英] Hook into System.out.println(); and modify
问题描述
我想修改System.out.println();
打印的输出.这怎么可能?
可能-我已经在Bukkit/Craftbukkit中看到了.如果插件正在使用System.out.println(String string)打印字符串; Bukkit将时间/日期和记录状态添加到字符串中.
我想像Bukkit一样.
I would like to modify the output printed by System.out.println();
. How is this possible?
It is possible - I've seen it in Bukkit/Craftbukkit. If a plugin is printing a string with System.out.println(String string); Bukkit adds a time/date and logging status to the string.
I would like to do the same like Bukkit.
推荐答案
您可以更改用作标准输出的PrintStream
:
You can change the PrintStream
that is used as the standard output:
System.setOut(PrintStream out)
创建自己的 PrintStream
实现,它将所需的任何额外信息打印到(旧)标准输出,并使用以下命令进行设置:
Create your own PrintStream
implementation which prints whatever extra info you want to the (old) standard output, and set it with:
System.setOut(myStream);
示例:
以下示例在使用PrintStream.println(String x)
方法:
The following example prints the current time millis before each printed String
that is printed with PrintStream.println(String x)
method:
PrintStream myStream = new PrintStream(System.out) {
@Override
public void println(String x) {
super.println(System.currentTimeMillis() + ": " + x);
}
};
System.setOut(myStream);
System.out.println("Hello World!");
输出:
1420553422337: Hello World!
注意:
此示例仅覆盖 PrintStream.println(String x)
方法,因此调用PrintStream
的其他打印方法不会将时间戳添加到输出中.
This example only overrides the PrintStream.println(String x)
method, so calling other print methods of PrintStream
would not add the time stamp to the output.
这篇关于钩入System.out.println();并修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!