Capturing Console Output (println) in Java
This from the good folks at Sun,
the Java Developers Connection.
If you don't yet
subscribe to JDC
you should -- it's free.
Quoting (with my valuable additions) from here >>>>>>>
This tip demonstrates how you can record in a log file everything you
print to standard out and standard error.
This is especially useful
if you deploy an application and your users encounter problems.
You can have the users send you the log file for analysis.
A SaveOutput object is a PrintStream object that acts like a tee.
Any characters it receives are forwarded to two places: the log file
and the underlying print streams.
SaveOutput extends PrintStream,
and is constructed on an existing PrintStream,
plus a second existing PrintStream,
and acts like a tee.
Any characters it receives are forwarded to two places:
the log stream and the underlying print stream.
The PrintStream class is deprecated vs PrintWriter,
therefore ALL constructors produce a deprecated notice!
But System still requires PrintStream for stdout and stderr,
so there is nothing we can do about it.
SaveOutput has two static methods - start () and stop ().
- start () creates a new log file,
or empties an existing log file.
It copies into the log file,
any characters printed to standard output and standard error.
The start () method saves the current standard output and
standard error print streams.
These print streams will be restored when stop () is called.
- stop () restores the original standard output and standard error.
It then closes the log file.
The underlying PrintStreams
are the original standard output and standard error;
these are supplied to the SaveOutput constructor.
The redirection applies to
all output sent to standard output and standard error,
regardless of what ".java"
compile unit it appears in.
The write methods do not throw exceptions.
Instead, they set a flag in the print stream if some problem occurs.
They set the flag by calling setError ().
If the client of the PrintStream wants to check if an error occurred,
it can call checkError.
Wish List:
- ability to set debug levels, zero being off, to allow finer control
- redirecting output to a file upon request;
- implementing an ASSERT (or VALID) mechanism if possible;
- minimizing the overhead when it is turned off.
- have each object know how to dump itself to a supplied Debug object.
Although the Sun example doesn't show further elaborations,
- wrappers could provide debug levels and assertion triggers,
while any "deeper" prints would still use the tee mechanism,
- could output to a message window instead of (or plus) a log file,
- could support filtering of what lines make it to which places.
Sources
This software is provided free of charge with no support.
Email
me at morris_hirsch@brown.edu
if you do have a problem,
and I will try to help,
but I cannot promise to.
Your comments,
suggestions,
bug reports,
bug fixes,
and enhancements,
are welcome!
Was this Helpful?
Was this Helpful?
Was it what you were looking for?
How did you find this page?
Search result,
following a link,
recommendation?
Please let me know.
Email
me at morris_hirsch@brown.edu
Please also visit my home page.
Legalities
Except for all sections which are identified as the work,
and copyright,
of others,
this work is Copyright (C) 1998 by Morris Hirsch.
All rights reserved,
except as granted here.
Redistribution and use in source and binary forms,
with or without modification,
for any purpose,
are permitted,
except as may be restricted by original copyright holders,
provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice,
all earlier copyright notices of others,
this list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright notice,
all earlier copyright notices of others,
this list of conditions and the following disclaimer,
in the documentation,
and/or other materials provided with the distribution.
DISCLAIMER
This software is provided free of charge with no support.
This software is provided in source format;
You are advised to examine it and understand it,
before putting it to use.
This software uses only algorithms and coding techniques
that are available to the public in the open literature.
You are however,
advised to make your own determination of legality,
for your intended use.
This software is provided by the Author and Contributors ``AS IS''
and any express or implied warranties,
including, but not limited to,
the implied warranties of merchantability
and fitness for a particular purpose,
are disclaimed.
In no event shall the Author or Contributors,
or their Agents,
be liable for any direct,
indirect,
incidental,
special,
exemplary,
or consequential damages
(including, but not limited to,
procurement of substitute goods or services;
loss of use, data, or profits;
or business interruption)
however caused and on any theory of liability,
whether in contract, strict liability,
or tort (including negligence or otherwise)
arising in any way out of the use of this software,
even if advised of the possibility of such damage.