|
System.out.print()
or
System.err.print()
statements, you can still use some of
the features of JavaLog without changing the code. JavaLog can be
installed as the System.out or err
stream such that all
uses of the the System.out or err
will be routed through
JavaLog and thus can be filtered.
Using the PrintCatcher allows you to use your existing code with print statements but still use indirection, formatting, and filtering of JavaLog. Currently, this functionality is experimental but will improve over time. Of course, it is better to use the actual JavaLog interfaces.
Here's how to use it: before making any calls to
System.out
or System.err
, instantiate and
install the special java.log.PrintCatcher:
import grace.log.PrintCatcher; ... public static void main(String[] args) PrintCatcher.install(); ... System.out.print("some starting text; now a number "); System.out.print(1); System.out.println(" some ending text."); System.err.println("some error text that now goes through JavaLog"); PrintCatcher.uninstall(); // if you want to go back to old standard out and err }
Notice in the above example, the data printed using
print(...)
(as opposed to println
) are
buffered until a println
call is made, and then the whole
previously buffered line is written as a single log event. This
should probably be the desired behavior for most users but it can be
changed using the PrintCatcher.setBufferedPrints()
function.
Also note that the PrintCatcher.install
and
PrintCatcher.uninstall()
can be called repeatedly to
alternate between using JavaLog or not using it for print statements.