Home arrow static arrow Java Programming [Archive] - Redirecting System.out System.err....
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Redirecting System.out System.err....
This topic has 4 replies on 1 page.

Posts:3
Registered: 6/18/04
Redirecting System.out System.err....  
Jun 18, 2004 2:16 PM



 
I am trying to redirect System.out and System.err to a log file. No problem, easy enough with:
        PrintStream logPS = new PrintStream (new FileOutputStream ("output.log");        System.setOut(logPS);        System.setErr(logPS);

However, I STILL want SOME things to display to the screen. How can I do this?

I tried making another PrintStream and setting it to Sytem.err as in:
          PrintStream errPS = System.err          PS = new PrintStream (new FileOutputStream ("output.log");          System.setOut(logPS);          System.setErr(logPS);           errPS.println("This SHOULD prints to the screen");          System.out.println("This SHOULD print to the log file");

I also tried making a file handle out of stderr (FileDescriptor.err) as in:
	    FileOutputStream stdErr = new FileOutputStream(FileDescriptor.err);            PrintStream errPS  = new PrintStream (new BufferedOutputStream(stdErr, 128), true);             PrintStream logPS = new PrintStream ( new FileOutputStream( "output.log" ) );              System.setErr( logPS );            System.setOut( logPS );           errPS.println("This SHOULD prints to the screen");          System.out.println("This SHOULD print to the log file");

But for both attempts, EVERYTHING goes straight to the log file as SOON as I do the "System.setErr(logPS)". Any ideas on how to i this?

TIA,
/mario
 

Posts:2,206
Registered: 8/15/02
Re: Redirecting System.out System.err....  
Jun 18, 2004 3:07 PM (reply 1 of 4)



 
I know it's possible using log4j, but I'm not sure how it works internally.
 

Posts:3,258
Registered: 00-08-28
Re: Redirecting System.out System.err....  
Jun 18, 2004 4:25 PM (reply 2 of 4)



 
I would think it this way. You could have different log levels. Fatal, error, warning, debug. Some of these logs you could print on screen and rest on console or other wise. Or you could enable or disbale the console output all together.
 

Posts:1,183
Registered: 1/23/02
Re: Redirecting System.out System.err....  
Jun 18, 2004 5:06 PM (reply 3 of 4)



 
The last example works on my PC (1.3.1). What JRE are you using?
 

Posts:2,206
Registered: 8/15/02
Re: Redirecting System.out System.err....  
Jun 18, 2004 5:06 PM (reply 4 of 4)



 
What if you want all unchecked exceptions to go to the log file AND to the screen?+
 
This topic has 4 replies on 1 page.