Passing System.out/System.err as parameters

I'm sure there must be a simple answer to this that I'm just not seeing, suggestions please.
I have a method in a logging utility that takes either a PrintWriter or a Printstream parameter as a target for logging and a second method that closes the log.
In principle either System.out or System.err could be passed in either directly or wrapped in a PrintWriter.
In the closing method I want to determine if one of these two PrintStreams were passed and if so skip the close() operation so as not to lose connectivity to stdout or stderr.
In the case of the PrintStream parameter I can use an identity check and condition a boolean to control closing the file.
The problem is the PrintWriter, if the utility user wraps these PrintStreams.
1: As I understand it closing the PrintWriter will cause the underlying OutputStream to close.
2: Since it's a protected field the underlying OutputStream is not visible to me.
3: I don't wish to subclass PrintWriter if there's another way to solve the problem.
Any ideas?

Why don't you use a BufferedWriter() ?

Similar Messages

  • One JVM with different system.out / system.err files

    I have a menu application which allows me to launch different swingapps which runs inside one single JVM.
    All applications have their own properties.file which is read and handled at the start of each application.
    One property allows me to print all kind of system.err / system.out which i want to redirect to a specific file. This is implemented with following code:
            if (isTRACE_ENABLED()){
                try {
                    setTrace_out_log(new PrintStream(
                              new BufferedOutputStream(
                                    new FileOutputStream(props.getProperty("TRACE_OUTLOG_FILE")),128), true));
                    System.setOut(getTrace_out_log());
                    setTrace_err_log(new PrintStream(
                            new BufferedOutputStream(
                                  new FileOutputStream(props.getProperty("TRACE_ERRLOG_FILE")),128), true));
                    System.setErr(getTrace_err_log());
                } catch(IOException e) {
                    e.printStackTrace();
            }This works file but... all system.out and system.err is redirected to same file... which is not what i want.
    Example:
    debug property for menu application = enabled
    debug property for app 1 = disabled
    debug property for app 2 = enabled
    In above case i want to have 4 new files:
    - menuapp_out
    - menuapp_err
    - app2_out
    - app2_err
    This doesn't work, the files are created but after starting app2, the print-statements for the menu application are alse redirected to the app_2_xxx files. And when i finish app2, i do not get any print-output anymore
    IMHO this is because the JVM only has 1 system.out and 1 system.err file. Is there some way to solve this?

    I understand that i need to use java.util.logger (JUL) or Log4j
    Are there any (free) tools availabe to read/analyze the logfiles created by above tools?

  • OPMN system.out and system.err log rotation?

    I would like to rotate the log generated by OPMN by default for system.out & system.err, which is named:
    OC4J~APPNAME~home~default_island~1
    I see the entries in the opmn.xml file for the ons.log and ipm.log which ARE being rotated properly. I've read that you can redefine the log file for system.out and system.err, but can't find any about rotating these logs.
    Does anyone have any info about this topic?

    If anyone's curious and is trying to do this themselves, I eventually asked Oracle, who stated it could not be done.

  • System.out Problem in Java Plug-in

    I have the following problem, anyone can help me?
    I have an application( Frame ) needed to be embedded in applet. The applecation has a lot of information output to console( by System.out ). When I embeded it into applet, these information will be output to Java Plug-in Console. In the beginning, there is no problem. But when the applet runs enough time, the output will be more and more, and the speed of the applet( in fact, the browser ) runs slower and slower.
    I don't want to modify my application at all( it is impossible ). I wonder there is any good suggestion to this problem, I guess, maybe there is an interface to make the Java Plug-in clear its console buffer periodically.
    Thanks in advance.

    There is a good reason for this.... the console in the java plug-in is essentially a JTextArea... and continually adding text to it via System.out.println or the like will make it slower the longer it runs..... periodically clearing the console would be the only solution without modifying the code......
    If you still need all that information you could redirect System.out to your own PrintStream and then instead of writing to the console little by little, you could save the original System.out (using something like:
    PrintStream origSysOut=System.out;
    ) and then later on in your app reset System.out=myPrintStream or something.... then just write the myPrintStream data to origSysOut every now and then or something....
    if you want a better example of this I've got a log facility class that I built based on this principal... it's also and easy way to capture stack traces into log files... and since you're just redirecting System.out or err it's easy to turn off the log file to use for debugging.... ok that's enough of a rant :)

  • Urgent: Redirecting System.out.println to more than one place

    Hi All,
    I want to divert all my System.out.println() statements to a Log file as well as on to the screen.
    But only one of the 2 options actually works.
    So if anyone can suggest me some ways I will appreciate very much.
    My code looks something like this:-
    if (m_filename != null) {
    try {
    FileOutputStream fos = new FileOutputStream(m_filename);
    PrintStream ps = new PrintStream(fos);
    System.setOut(ps);
    System.setErr(ps);
    //This code Added for Printing Onto Screen But it does not work
    Stream ps1 = new PrintStream(System.out);
    System.setOut(ps1);
    System.setErr(ps1);
    } catch (IOException ioe) {
    System.out.println("Could not create log file " + m_filename + " because " + ioe);
    As far as logging onto file is menat it works when i set it but printing onto screen does not.
    How can i achive it
    Thanks
    Raj

    Hi,
    You are right but in my current scenario i cannot incorporate this log4j because my application is using some other framework and I am just trying to override this frameworks loggig behaviour so that it logs to both a log file as well as on to the screen.
    Here is the code that this framework uses for logging and i am simply overriding it to do the reqd thing but its not working for me:-
    * Resets the System's out and err OutputStreams to the Launcher's
    * own ThreadedOutputStream, so that output from different threads
    * can be tracked efficiently.
    protected void setOutputStreams() {
         if (scInfo.size() <= 1) {
    // don't need Threaded handling with one thread
    if (m_filename != null) {
    try {
    FileOutputStream fos = new FileOutputStream(m_filename);
    PrintStream ps = new PrintStream(fos);
    System.setOut(ps);
    System.setErr(ps);
    //Added for Printing Onto Screen
                             PrintStream ps1 = new PrintStream(System.out);
    System.setOut(ps1);
    System.setErr(ps1);
    } catch (IOException ioe) {
    Debug.fatalError("Launcher.setOutputStreams","Could not create log file " + m_filename + " because " + ioe);
    else {
    Debug.information("Launcher.setOutputStreams","Initializing ThreadedPrintStream");
    if (m_filename == null) {
    System.setOut(new ThreadedPrintStream(System.out));
    System.setErr(new ThreadedPrintStream(System.err));
    else {
    PrintStream out = new ThreadedPrintStream(m_filename);
    System.setOut(out);
    System.setErr(out);
    //Added for Printing Onto Screen
    System.setOut(new ThreadedPrintStream(System.out));
    System.setErr(new ThreadedPrintStream(System.err));
    Debug.information("Launcher.setOutputStreams","ThreadedPrintStream initialized.");
    Thanks
    Raj

  • How to capture System.err and System.out in a method?

    Is there some way to capture everything that is sent to System.err or System.out and have it instead go to e.g. a method as a String?
    The reason I want to do is this: I am invoking some method from a class that sends output to System.err and System.out but while that method is run, I want everything that goes to System.out or System.err to instead go to a String buffer or a Swing Scroll Pane.
    What is the easiest way to do this?

    I want everything that goes to System.out or System.err to instead go to a String buffer or a Swing Scroll Pane.Then maybe you should be searching (and then posting) in the Swing forum. Thats where I've seen this question asked and answered many times in the past.

  • System.out and System.err

    Hi all,
    This is a stupid newbie question, but I could not find the answer on the Web
    site, so here goes: Where do System.out and System.err get written to? I'm
    trying to deploy some plain-vanilla stateless session beans that do a bunch
    of println() calls, but I can't see the output anywhere! The WebLogic
    Console shows no messages, /myserver/weblogic.log has nothing interesting,
    and there are no .log files anywhere that I can see. I even searched /tmp
    and found nothing of interest. What am I missing? Do I have to explicitly
    open a file for System.out and/or System.err? That doesn't sound right...
    - danz

    The simple answer to your questions are no and no.
    I recently logged a problem with BEA WebLogic technical support regarding
    this issue and their response is:
    You have two choices. You can either use standard Java file i/o to write
    your output to a file, or you can use our t3loggingservices to append
    messages into the weblogic.log
    The "jump point" for the logging services is at --
    http://www.weblogic.com/docs51/classdocs/javadocs/weblogic/common/LogService
    sDef.html
    It is actually very easy to use -- after you import the proper packages into
    your web application it is just as easy to use as System.out.println.
    John J. Feigal Voice (651)766-8787 (main)
    Sr. Technical Consultant (651)766-7249 (direct)
    Ensodex, Inc. Fax (651)766-8792
    4105 N. Lexington Ave., Suite 150 email [email protected]
    Arden Hills, MN 55126 WebSite http://www.ensodex.com
    "Jon Wynett" <[email protected]> wrote in message
    news:[email protected]...
    I'm running WebLogic as an NT Service. Is there any way to see the
    System.out.println messages? Can they be redirected to the weblogic.log
    file?
    We were running through a DOS Window and saw all the messages, however we
    ideally want to run Weblogic as a service.
    "Rob Woollen" <[email protected]> wrote in message
    news:[email protected]...
    I'm guessing that you started the server with the .exe file on Windows.
    If you're debugging with printlns, it's generally more conventient to
    use the startWebLogic.sh or startWebLogic.cmd files to start the server
    from a shell.
    By default, you'll see stdout and stderr in the window.
    -- Rob
    Dan Zivkovic wrote:
    Hi all,
    This is a stupid newbie question, but I could not find the answer on
    the
    Web
    site, so here goes: Where do System.out and System.err get written to?I'm
    trying to deploy some plain-vanilla stateless session beans that do abunch
    of println() calls, but I can't see the output anywhere! The WebLogic
    Console shows no messages, /myserver/weblogic.log has nothinginteresting,
    and there are no .log files anywhere that I can see. I even searched/tmp
    and found nothing of interest. What am I missing? Do I have toexplicitly
    open a file for System.out and/or System.err? That doesn't soundright...
    - danz

  • Redirecting System.out/err

    Hi all,
    Is there an easy way to redirect the System.out/err stream to my own stream? I want to implement that everything going to System.out/err will be printed in a JTextArea instead of the console (Like JBuilder does for example).
    Thanks for any hint, Mathias

    System.setErr(myErrorOutputStream);
    System.setOut(myStdOutStream);

  • System.out and System.err  How to get to show up in log

    Does anyone know if there is anyway to get System.out and System.err
    messages to appear in the log?
    Trying to build and debug a JSP project is a complete nightmare when the
    remote developers cannot see System.out or System.err messages from helper
    classes.
    Platform= Windows NT 4.0
    Weblogic running as a Service
    Thanks in advance!

    Write a wrapper class to redirect the std out to what ever stream you want.
    HTH
    Saman

  • System.out and System.err to files

    Hi !
    Could somebody tell me, how I can forward all System.out.println and System.err.println Messages from
    my Java modules (EJB, Servlets) to different files respectively.
    Thanks
    Steve

    I think you could create PrinStreamS from your desired output files and then use System.setOut(<...>) and System.setErr(<...>). Place this code in a servlet that you load at startup...

  • System.out vs. System.err -- best practices?

    When I catch an exception, where is the best place to write the stack trace, etc? Should I write it to System.out or System.err?
    Thanks,
    Curt

    If you call printStackTrace() it will by default print to the to the standard error stream.
    But really you should look at log4j.

  • System.out vs System.err questions

    Hello,
    System.out and System.err both seem to direct regular and error output to the stdout. I have the following questions:
    1. I used System.setOut() to direct regular output to "Output.txt" and System.setErr() to direct errors to "Errors.txt". I purposely made the program to throw some exceptions, wanting to redirect them to "Errors.txt". It did not work. My question is "What kind of errors setErr() would direct to an error file?"
    2. Without using setErr(), errors go to stdout. My question is "Does Java hava a configuration file I can configue to direct regular output to stdout, but errors to a user-defined file such as errors.log?"
    Thanks!

    I see the stack trace in file err.txt:
    import java.io.*;
    public class Example {
        public static void main(String[] args) throws FileNotFoundException {
            System.setOut(new PrintStream(new FileOutputStream("out.txt"), true));
            System.setErr(new PrintStream(new FileOutputStream("err.txt"), true));
            System.out.println("<stdout>");
            System.err.println("<stderr>");
            throw new RuntimeException("here we go");
    }

  • System.out/err log file redirection

    Hi,
    Do u know if System.out/err are redirected in
    a log file with Oracle 9ias 9.0.4 (Production Release) ?
    If yes, which one ?
    Thanks

    You may be looking for the stdout/stderr process output.
    Almost all components in Oracle Application Server 10g
    (9.0.4) are managed (at the process level) by OPMN. To
    preserve the stdout and stderr of every process, all
    processes started by OPMN have their stdout/stderr
    redirected to a file (one file for each running process).
    This file is sometimes referred to as the "console log".
    Console logs are located in $OH/opmn/logs and have a
    filenames containing '~' chars. Identifying which
    console log file is associated with which process
    should be intuitive.
    When a process terminates and is replaced by a new
    process, console log output from the previous process is
    preserved and the replacement process appends to the
    end of the console log file.

  • Temporarily routing System.out and System.err to a String

    Hi everybody,
    Does anyone know of a way to temporarily route System.out and System.err directly from the console into a String, then set it back to its default to the console? I know how to reroute to a text area:
    TextAreaOutputStream taos = new TextAreaOutputStream(yourTextArea);
    PrintStream ps = new PrintStream(taos);
    System.setErr(ps);
    System.setOut(ps);But it doesn't seem like there is either a way to reroute to a String or a way to switch back to the default routing afterward. Can anyone give me some advice on how to do this, please?
    Thanks,
    Jezzica85

    Too late to the party?
    import java.io.*;
    public class Redirection {
        public static void main(String[] args) {
            System.out.println("To console");
            PrintStream old = System.out;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PrintStream ps = new PrintStream(baos);
            System.setOut(ps);
            System.out.println("To memory");
            System.out.print("No 'ln'");
            //System.out.flush(); //needed?
            System.setOut(old);
            System.out.println("To console again?");
            System.out.println("earlier I wrote...");
            byte[] data = baos.toByteArray();
            String message = new String(data);
            System.out.println(message);
    }

  • Rerouting System.out and System.err

    Hi everybody,
    I'm trying to make a small application with an agnostic back end that can print out its progress messages to either the console or a JTextArea, depending on whether the console front end or GUI front end is called. I originally created a small utility class that the agnostic back end extended, in which one could set a variable which told it to use the JTextArea or console. The solution seems a bit clumsy to me, though, so it crossed my mind; does anyone know if there is a way, in the main method of a program, to reroute System.out and System.err to a desired location?
    Basically, I'd like functionality like this:
    Console front end:
    1. Start main
    2. Create object for back end
    3. All calls to System.out and System.err in back end route to console, as usual
    GUI front end:
    1. Start main
    2. Create Object for back end
    3. Create GUI
    4. Set System.out and System.err to show up in GUI text field
    5. All calls to System.out and System.err in back end route to text field in GUI
    Is this possible? I have my somewhat hacked solution working fine, but I don't want to start ripping it apart too badly until I know if something simpler like this can work. Can anyone point me in the right direction, please?
    Thanks,
    Jezzica85

    Thanks for the explanation scphan,
    In that case, I don't think that's exactly what I'm looking for.
    To answer both your question and cotton's:
    What I want to to is directly reroute System.out and System.err to a text area, only if I'm using a GUI. I'll try and explain it all, so if it gets long please bear with me--
    I have 3 classes, Calculate, Console, and GUI.
    Calculate is the back end of my application. Console is a front end class that allows the actions of Calculate to be run on the command line. GUI is a front end class that allows the actions of Calculate to be shown in a text area.
    I currently have a class, Output, that Calculate extends, and uses to print out progress/error messages. Output has a JTextArea data member, and print/println data members. If the JTextArea is null, calling the print methods prints to the console, and if not, the print methods print to the text area. So, at the current moment, I have a solution similar to that which scphan described (although I didn't recognize the terminology at first--apologies, it's almost 3 AM here).
    What I was wondering is if there is a simpler, more elegant way to do this. I have a few different small applications that have a similar setup to this one, and all the background classes must extend the Output class to work. If creating a class that every back end class must extend is the best way to do this, then I won't mess with what I have, but if there is a more elegant solution I've overlooked, I'd like to use that instead.
    I hope that's clear, sorry if there was any confusion.
    Thanks,
    Jezzica85

Maybe you are looking for

  • How can i import my old website into iweb

    hi, my harddrive crashed and I lost all my data. When I set up OSX for new I downloaded my website (www.et-voila.net) from my ftp and I wanted to import to i-web. But I found no possibility do this. How can I work on my old (i-web made) Website with

  • ITunes will not start up in Windows XP SP2 and maxes out CPU

    Hello all, Apologies if this has been covered elsewhere, or if this is a similar problem faced by others. I have browsed the forums for hours trying to find a similar issue to my own, and have tried several solutions but none have worked. I have lost

  • Getting rid of MyWebSearch - not toolbar, just search engine

    It's not a huge deal, but it's quite annoying: I believed I got rid of MyWebSearch from my computer completely (it's not on my add-remove programs list) and there isn't even a toolbar that I can see on Firefox or IE. However, whenever I try to search

  • IPad Mini to be used in a different country.

    Hello! Can I buy IPad Mini in the U.S. and then use it in Russia with a Russian SIM Card? Would I have the localization? Thank you.

  • How much time is on that Playlist-Burning CD

    Is there a better, even slightly more sophisticated way of determining just how much time is on a playlist? For burning to a CD? I'm trying to burn a CD that holds 80 minutes, or 700 MB. I know that what that really means is that the CD can accommoda