This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange behavior piping System.out to file.

 
Cindy Moncsko
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
David Freels
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try posting some code or give a little more information please.
David
 
Cindy Moncsko
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic