我正在使用外部图书馆。当一个调用这个库的方法时,它会在控制台上输出一些文本。我想从控制台隐藏此文本。这怎么可能?
提前感谢
我正在使用外部图书馆。当一个调用这个库的方法时,它会在控制台上输出一些文本。我想从控制台隐藏此文本。这怎么可能?
提前感谢
你可以重新定义system.out(我相信它是system.setOut())我相信你可以将它设置为NULL(已更正--你不能将它设置成NULL),但你可以将其设置为任何输出流。
我曾经用这个做了一些有趣的事情。我保存了“System.out”,然后用“print”方法中的代码将其重定向到我自己的输出流——每当有人打印到流时,就会调用该方法。
每当有一行输入到这个类时,我都会创建一个堆栈跟踪,获取跟踪并深入到调用System.out.println()方法的方法。在这一点上,我可以准备行并具有即时记录器功能——它甚至显示行号。
这可能很慢,但它可以很容易地打开和关闭。
我也可以在不接触源代码的情况下完成你想要的所有过滤。有时我会过滤一个包,有时我会筛选一个类,有时它会是以“BK:”开头的字符串,这只会打印出我的消息。
总的来说,删除生产中的所有调试输出既有趣又琐碎。
(我不建议生产代码使用获取堆栈跟踪,尽管我没有注意到,但它确实应该很慢)
// class variable
public static final OutputStream out;
{
out=System.getOutputStream();// I may have the actual name wrong, but it s close
System.setOutputStream(new OutputStreamThatDoesNothing());
}
此时,任何调用:
Redirect.out("Hello");
应该像调用System.out那样操作。
既然这没有魔法,如果你真的想,你也可以做这样的事情:
OutputStream tmp=System.getOutputStream();
System.setOutpuatStream(nullStream);
callOffensiveLibraryMethod();
System.setOutputStream(tmp);
这只会消除该调用中的输出,但如果您的应用程序是多线程的,则会非常糟糕。
private PrintStream realSystemOut = System.out;
private static class NullOutputStream extends OutputStream {
@Override
public void write(int b){
return;
}
@Override
public void write(byte[] b){
return;
}
@Override
public void write(byte[] b, int off, int len){
return;
}
public NullOutputStream(){
}
}
void someMethod(){
System.setOut(new PrintStream(new NullOutputStream());
realSystemOut.println("Hello World!"); //prints Hello World!
System.out.println("Hello World!"); //nothing!
System.setOut(realSystemOut);
System.out.println("Hello World!"); //prints Hello World!
}
对于那些寻找2020版本的人来说,该版本也不需要空流的外部库:
PrintStream out = System.out;
System.setOut(new PrintStream(OutputStream.nullOutputStream()));
//
//do stuff with library...
//
System.setOut(out);
从Java 11开始提供。
将System.out设置为NullOutputStream。apache.commons有一个可用的。
如果你想相应地打印出来,你可以写一个有趣的破解。
private static final PrintStream SYSTEM_OUT = System.out();
public static void debug(String debug){
SYSTEM_OUT.println(debug);
}
代码中的其他部分
System.setOut(new PrintStream(new NullOutputStream()));
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...