stdout being redirected to an open file?

Discussion in 'Java' started by bjhartin@yahoo.com, Oct 24, 2005.

  1. Guest

    All,

    Does anyone know why text sent to stdout might intermittently get sent
    to an open file? I have some code which writes data to a file and logs
    its activity using log4j. On Friday, the log statements began to
    intermittently appear in the file. I know the following things are
    true:

    * The code did not change
    * The configuration did not change
    * The log4j Logger is mapped only to a ConsoleAppender, i.e. log
    statements are written only to stdout
    * The name of the file is always unique

    Here is the code:

    //Create a unique filename for the order
    filename = "Order " + orderId;

    //Create the file
    logger.info("Creating file " + fileName);
    File file = new File(fileName);
    FileWriter fw = new FileWriter(file);
    logger.info("Writing File Content ");
    fw.write(fileContent + System.getProperty("line.separator"));
    logger.info("Closing File ");
    fw.close();

    It smells to me of an O/S or app server problem in which the file
    handle for the open file is being provided for calls to stdout. Has
    anyone ever seen this?

    Thanks,

    Brian
    , Oct 24, 2005
    #1
    1. Advertising

  2. Roedy Green Guest

    On 24 Oct 2005 08:55:57 -0700, wrote, quoted or
    indirectly quoted someone who said :

    >Does anyone know why text sent to stdout might intermittently get sent
    >to an open file? I have some code which writes data to a file and logs
    >its activity using log4j. On Friday, the log statements began to
    >intermittently appear in the file.


    See http://mindprod.com/console.htm

    You can dynamically redirect System.out and System.err.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 25, 2005
    #2
    1. Advertising

  3. Roedy Green Guest

    On Tue, 25 Oct 2005 04:40:27 GMT, Roedy Green
    <> wrote, quoted or
    indirectly quoted someone who said :

    >>Does anyone know why text sent to stdout might intermittently get sent
    >>to an open file? I have some code which writes data to a file and logs
    >>its activity using log4j. On Friday, the log statements began to
    >>intermittently appear in the file.

    >
    >See http://mindprod.com/console.htm
    >
    >You can dynamically redirect System.out and System.err.


    oops, did it again. That should be.

    http://mindprod.com/jgloss/console.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 25, 2005
    #3
  4. Guest

    Hmm, I'll check to see if any other code is redirecting stdout. I
    would assume that this is done per-JVM and not per-thread, right? This
    code is running as part of a POJO that backs a stateless session bean,
    so there are definitely other threads going in the JVM. However, I'm
    not sure how they could redirect stdout to the particular file I'm
    writing to, since it is created with a unique name each time the method
    is called.

    Thanks for your reply.

    Brian
    , Oct 25, 2005
    #4
  5. Roedy Green Guest

    On 25 Oct 2005 08:49:42 -0700, wrote, quoted or
    indirectly quoted someone who said :

    > I
    >would assume that this is done per-JVM and not per-thread, right?

    there is only one static copy of Sysout.out.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Oct 26, 2005
    #5
  6. Roedy Green Guest

    On 25 Oct 2005 08:49:42 -0700, wrote, quoted or
    indirectly quoted someone who said :

    >Hmm, I'll check to see if any other code is redirecting stdout. I
    >would assume that this is done per-JVM and not per-thread, right? This
    >code is running as part of a POJO that backs a stateless session bean,
    >so there are definitely other threads going in the JVM. However, I'm
    >not sure how they could redirect stdout to the particular file I'm
    >writing to, since it is created with a unique name each time the method
    >is called.


    so you don't really want to mess with Sysout. You simply want to
    direct some encoded character output to a file. For how to do that see
    http://mindprod.com/applets/fileio.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Oct 26, 2005
    #6
  7. Guest

    Perhaps I haven't explained the problem well. We're not having trouble
    writing to a file. That part is fine. The problem is that in addition
    to the expected data, the files intermittently contain data that was
    destined for stdout. You can run the same code ten times and see the
    problem only once or twice. In addition, the log statements which
    unexpectedly appear in the file are often out of order, as if they're
    being 'held up'. This does *not* reflect multithreading, because each
    file is given a unique name created from a database sequence.

    Since my original post I have discovered that other code on this box
    has had trouble writing to stdout. Sometimes the output just vanishes,
    while in our case it shows up in a file. I think we have a problem
    with the app server, JVM or O/S.

    Thanks for your replies.
    , Oct 26, 2005
    #7
  8. Roedy Green Guest

    On 26 Oct 2005 10:07:45 -0700, wrote, quoted or
    indirectly quoted someone who said :

    >In addition, the log statements which
    >unexpectedly appear in the file are often out of order, as if they're
    >being 'held up'.


    This sounds possibly like you are logging with many threads but your
    logging class is not thread safe. See
    http://mindprod.com/jgloss/threadsafe.html

    As for System.out stuff getting mixed in, your first job is to track
    which line in the code could have generated those lines, and make sure
    System.out is not redirected at that point or if those lines are being
    logged instead of System.outed.

    Unless you are using some home-grown spooling software, I would put
    blaming generic spooling low on my list of suspects. The problem would
    likely show up all over the place, not just this program if that were
    the source of the problem.


    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Oct 26, 2005
    #8
  9. Guest

    >This sounds possibly like you are logging with many threads but your
    >logging class is not thread safe.


    As I mentioned, we are using log4j with no customizations. log4j
    claims to be thread-safe. The log4j code has syncrhronized blocks of
    code for accessing resources for which there might be contention. At
    any rate, our log4j configuration specifies only console logging, for
    which contention should not be an issue, right?

    >As for System.out stuff getting mixed in, your first job is to track which line in the
    >code could have generated those lines, and make sure System.out is not
    >redirected at that point or if those lines are being logged instead of System.outed.


    I did a search and System.setOut appears nowhere in our codebase, nor
    in the log4j code. The code which generates the lines in question is
    simply that I described in my first message, i.e. simple calls to
    log4j's Logger.info() method.

    >Unless you are using some home-grown spooling software, I would put
    >blaming generic spooling low on my list of suspects. The problem would
    >likely show up all over the place, not just this program if that were
    >the source of the problem.


    As I mentioned above, I recently discovered that other java apps on
    this machine *are* having intermittent trouble writing to stdout.
    However, the common thing among these apps is that they run under
    Weblogic 8.1. Each app runs in its own JVM, i.e. its own Weblogic
    server (keep in mind Weblogic servers are virtual).

    Thanks for your help.
    , Oct 27, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben_
    Replies:
    0
    Views:
    328
  2. Pappy
    Replies:
    3
    Views:
    375
  3. Dirk Loss
    Replies:
    3
    Views:
    323
    Bjoern Schliessmann
    Dec 19, 2007
  4. Noel Sant

    Closing and re-opening redirected STDOUT

    Noel Sant, Feb 25, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    117
    Anno Siegel
    Feb 25, 2004
  5. Replies:
    2
    Views:
    322
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page