Listener to prevent System.out display on the screen
I was doing my academic project and while building and testing i have put many println() statements.
But when I had to submit all prints should not be displayed.
Can i implement something like listener which will be invoked when System.out is tried to be executed and prevents it from displaying. I dont know how feasible this idea is but just want to know whether its possible or not. I know i could have used a log file or write into a file but again its just a thought came into my mind if I have to disable SOP how can i do it ..
thanks
use System.setOut function (and setErr) The following program will only print 1: (and not 2)
public static void main(String[] args) throws FileNotFoundException {
System.out.println("1");
System.setOut(new PrintStream(new OutputStream() {
@Override
public void write(int arg0) throws IOException {
// TODO Auto-generated method stub
}
}));
System.out.println("2");
}
The correct thing to do is to either use flags before printlns, or better yet, to use a Logger (there are many versions available).
It is, however, possible to reroute all System.out away. Search for "redirect system.out" and you will find plenty of examples.
Use log4j. This will allow you to do something like:
if (log.isLoggingEnabled()) {
log.debug("this will be printed at runtime with debugging enabled");
}
You can then configure your application not to print debug statements after you submit your code (this is generally done in either a .properties file or an XML file). using a logger is a very common practice on websites running with Java and there is quite a bit of documentation on log4j in particular out there. In fact, submitting code with System.out lines for debugging is considered bad form in most circles. Think of the person reading the logfiles!
There is a TINY hit to performance to the extra method invocation using the logger, but doing this will make maintainability much more practical. You'd probably impress your instructor too :)
It's generally not a good idea to hard-code writing to System.out. As a quick fix, you could change all references to System.out to your own static variable in one of your classes. This at least gives you the opportunity to change the stream you are writing to. E.g.
public static void main(String[] args)
{
static public PringStream out = System.out;
void someMethod()
{
out.println("some logging message");
}
}
You could quickly replace all uses of System.out in your code with Myclass.out. With this in place, you can then change the output stream according to arguments, or system properties. E.g.
if (Boolean.getBoolean("debug"))
out = System.out;
else
out = new PrintStream(new OutputStream() {
public void write(int data) throws IOException {}
};
Of course, this is all seat-of-the-pants stuff and throwaway code.
A more robust and flexible solution is to use a logging api, like slf4j.
PrintStream realOut = System.out;
try {
out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
System.out.println("bla");
} finally {
System.setOut(realOut);
}
System.out.println(out);
精彩评论