jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes Strange behavior piping System.out to file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Strange behavior piping System.out to file." Watch "Strange behavior piping System.out to file." New topic
Author

Strange behavior piping System.out to file.

Cindy Moncsko
Greenhorn

Joined: Aug 11, 2000
Posts: 7
Hello,
I have a utility class that pipes System.out to a file. I'm using it to debug a servlet. The output is captured in the file, but each line is repeated 24 times. In my (non servlet) appliations, the same utility only prints each line once. Does anyone have an idea why the lines are repeated?
Thanks!


Cindy
David Freels
Ranch Hand

Joined: Feb 01, 2001
Posts: 102
Try posting some code or give a little more information please.
David
Cindy Moncsko
Greenhorn

Joined: Aug 11, 2000
Posts: 7
Your right code would help
The StdOutToFileLog class is the one I'm using to copy the System.out lines into a file. The serverlet creates an instance of this class in its service method. Every time the code does a System.out.println("what ever") the line is repeated 24 times in the log file.

/************* Send System.out and err to log file
/* Derived from:
* Oct 21, 1999 Sun Java Developer's Connection Technical Tip
*/
public class StdOutToFileLog extends PrintStream {
public StdOutToFileLog(OutputStream out, boolean autoFlush) {
super(out, autoFlush);
}
public static void start(String filename) {
try {
oldStdout = System.out;
oldStderr = System.err;
logfile = new PrintStream(new BufferedOutputStream(new FileOutputStream(filename)),true);

/* Start redirecting to output */
out = new StdOutToFileLog(System.out,true);
System.setOut(out);
err = new StdOutToFileLog(System.err,true);
System.setErr(err);
...
/* */
public void write(byte[] buf, int off, int len) {
try {
logfile.write(buf,off,len);
logfile.flush();
super.write(buf,off,len);
...
/* */
public void write(int b) {
try {
logfile.write(b);
logfile.flush();
super.write(b);
...
public static void stop() {
try {
/* Redirect the output back to original location */
System.setOut(oldStdout);
System.setErr(oldStderr);
/* writes the output to the file */
logfile.close();
...
/************ Servlet code
...
public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
StdOutToFileLog.start(File.separatorChar + "tmp" + File.separatorChar + "IfsSessKidLog");
System.out.println("Anything");
...
StdOutToFileLog.stop();
...
David Freels
Ranch Hand

Joined: Feb 01, 2001
Posts: 102
Try adding a static int counter to the servlet. Anytime the service method of the servlet is called, increment the variable. Call the servlet three times. The counter should be three, if not tell me servlet container you are using. (IE JRun, Tomcat, etc..)

David
Sun Certified Programmer for the Java2 Platform
Cindy Moncsko
Greenhorn

Joined: Aug 11, 2000
Posts: 7
I added the counter, which seems to count as expected. However, now I can not seem to reproduce the problem. The servlet is printing one copy of each line regardless of whether I invoke it from a web browser or an application, and regardless of whether I use HttpServletRequest(Response) or ServletRequest(Response). If the problem shows up again I'll retry your suggestion. David, thank you for your responses! As someone new to servlets, I appreciate your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange behavior piping System.out to file.