The Artima Developer Community
Sponsored Link

Java Answers Forum
Redirecting System.out.println

8 replies on 1 page. Most recent reply: Apr 6, 2002 4:14 AM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
Lois

Posts: 4
Nickname: lois
Registered: Mar, 2002

Redirecting System.out.println Posted: Apr 1, 2002 10:51 AM
Reply to this message Reply
Advertisement
Hi,

How can System.out.println messages from all classes be displayed into the JList of a new class DisplayOutput be done?


Thanks,
lois


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Redirecting System.out.println Posted: Apr 1, 2002 1:12 PM
Reply to this message Reply
System.setOut(PrintStream out)

Lois

Posts: 4
Nickname: lois
Registered: Mar, 2002

Re: Redirecting System.out.println Posted: Apr 2, 2002 11:00 AM
Reply to this message Reply
What should I instantiate for the PrintStream?


lois

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Redirecting System.out.println Posted: Apr 2, 2002 1:06 PM
Reply to this message Reply
Here is an example of how you would do it:
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.FileNotFoundException;
 
public class NonStandardOut
{
    public static void main( String[] args )
    {
        String filename = "test.out";
        if( args.length > 0 )
            filename = args[0];
 
        try
        {
            PrintStream outStream = new PrintStream( new FileOutputStream(filename) );
            System.setOut(outStream);
            System.out.println("This is a test!");
            outStream.close();
        }
        catch( FileNotFoundException fnfe )
        {
            System.out.println("Sorry, can't use the file " + filename + "." );
        }
    }
}

Lois

Posts: 4
Nickname: lois
Registered: Mar, 2002

Re: Redirecting System.out.println Posted: Apr 2, 2002 2:15 PM
Reply to this message Reply
And then displaying the contents of the file to the JList component?

Can the output stream be the JList component?


Thanks,
lois

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Redirecting System.out.println Posted: Apr 2, 2002 11:06 PM
Reply to this message Reply
Well in that case, you could create your own PrintStream or OutputStream objects and override the appropriate methods, but I think that would be a lot more work than simply refactoring all your code to use a method you define in place of System.out.println().

That is, write a MyUtils.println() or something of your own and do a search and replace of System.out.println() with myUtils.println() in all your code. Then, you can make your println() method smart enough to send the line to System.out.println() and/or your listbox and/or a specified file and/or a Logger, etc. (based upon a property setting, for example). Of course, your println() must somehow be aware of the ListModel that it supposed to update, so it should probably not be a static method and the MyUtils object should probably have a constructor that receives the target of its println()s.

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Redirecting System.out.println Posted: Apr 5, 2002 3:24 AM
Reply to this message Reply
My problem with all the answers you get from the Forum is that you are probabaly trying to solve in a proper way a simple Logging issue.
I believe you may then gain a lot of assless by having a look at : http://jakarta.apache.org/log4j

Hope you can jump the gap if it's your issue
:-)

Rgds,

thomas,

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Redirecting System.out.println Posted: Apr 5, 2002 1:52 PM
Reply to this message Reply
Well I usually try to answer the actual question, which in this case was not "how do I implement logging in my application?" On those occasions when I think I can glean the purpose behind a question, then I include a more appropriate suggestion, if I can. However, I think it is still important to ask the original question posed and not just try to divine the underlying purpose tell people to do it another way.

It would actually be nice if the people posing the questions would state their purpose instead of, or at least in addition to asking how to do a specific thing. Unfortunately, most people have a proclivity for keeping their purposes close to the vest and only asking how to do a specific thing.

By the way, the original question was about redirecting System.out to a file; I would guess that this wouldn't be for debugging purposes, since it would be hard to debug if you can't see what's going on until the program completed execution. But you never know.

As of version 1.4, the Java API has a logging facility, so I'd use that if I wanted to do some logging. This Log4j seems to be aimed at debugging; these days the debugging tools for Java a getting pretty good, so I'd recommend using a real debugging tool. IDEs like IntelliJ IDEA, Borland JBuilder, Forte for Java and NetBeans (which is free a open source IDE) all have nice debugging facilities, some including remote debugging of container code. If, for some reason, it was impossible to use a real debugger, it might be worth considering a code generator that runs in the build process (only for the debug version) that inserts the debug logging statements before compiling (in a temporary hierarchy, of course, so as not to affect the source code).

Finally what does it mean to "gain a lot of assless?"

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Redirecting System.out.println Posted: Apr 6, 2002 4:14 AM
Reply to this message Reply
>
> By the way, the original question was about
> redirecting System.out to a file; I would guess that
> this wouldn't be for debugging purposes, since it
> would be hard to debug if you can't see what's going
> on until the program completed execution. But you
> never know.
>

Well I believe(d) the redirection is to Monitor the application running. It's to me part of the debugging 'cause if you were 100 % sure of your app you would not need to monitor it, he !

Just a question of point of view I suppose

>
> As of version 1.4, the Java API has a logging facility, so I'd use that if I wanted to do some
> logging. This Log4j seems to be aimed at debugging; these days the debugging tools for Java a getting
> pretty good, so I'd recommend using a real debugging tool. IDEs like IntelliJ IDEA, Borland JBuilder,
> Forte for Java and NetBeans (which is free a open source IDE) all have nice debugging facilities, some
> including remote debugging of container code. If, for some reason, it was impossible to use a real
> debugger, it might be worth considering a code generator that runs in the build process (only for
> the debug version) that inserts the debug logging statements before compiling (in a temporary
> hierarchy, of course, so as not to affect the source code).
>

Yea, sure ...
but Log4J has been out for some times now & it's pretty stable. Also, I don't see why Logging should be part of the language "specs".
It's really smthg outside the language scope so ...
Also if a product like JBoss (http://www.jboss.org) uses Log4J it probably means that it's still better than any thing else, then ...

Log4J is not for debugging, though it may help a lot !
Log4J is for Logging.
You may sometimes need Logging to debugg but there certainly no way of getting confused between Logging & Debugging. It's like thinking that Unit testing is for debugging...

I am mentionning that cause I had an argument with a blonde CTO who could not make the difference :-D
I hope you are not ?
Nothing personnal, he


Out of scope
I would prefere Sun to fix the Multi-threading problem instead of dealing with solved matters.

>
> Finally what does it mean to "gain a lot of assless?"
>

Ooops,
I am not a native english (I took english as a third language in Secondary school so my apologies for this typo).
I mean(t):
It would spare you a lot of assless, though the second steps are sometimes a little bit hard.

Flat View: This topic has 8 replies on 1 page
Topic: toolbars & scrolling Previous Topic   Next Topic Topic: telephone

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use