0

I am working with mission critical applications. I would like to know what JVM argument I have to pass in order pump out and err stream into a file.

I have log4j as part of my application by using slf4j framework. Is there a way this can be achieved with log4j.

Dev Anand Sadasivam
  • 699
  • 7
  • 21
  • 49
  • 1
    Did you check [Redirect System.out.println to log](http://stackoverflow.com/questions/15403275/redirect-system-out-println-to-log) or [Want to make “System.out.println()” to print message to log4j](http://stackoverflow.com/questions/6566960/want-to-make-system-out-println-to-print-message-to-log4j) already? – Roland Mar 28 '17 at 07:39
  • Its not `cron`, in fact some runs with Weblogic 12c. – Dev Anand Sadasivam Mar 28 '17 at 09:21
  • 1
    If you have mission critical applications, why do they even write to `out` or `err`? where did you see `cron`... I didn't link or say anything about `cron`? But as you wrote something about Weblogic. May the following help you? [How to make WebLogic log all “console” message into a file?](http://stackoverflow.com/questions/8112581/how-to-make-weblogic-log-all-console-message-into-a-file) – Roland Mar 28 '17 at 11:22

1 Answers1

0
    ...
      //Save default stdout and change stdout to be written to a buffer instead of console
      PrintStream defaultOut = System.out;
      ByteArrayOutputStream buffer = changeSystemOutputStream();
      try {
        //Do Stuff that would write to stdout
      }
      finally{
        //Change stdout back to default and write the log messages to file
        revertSystemOutputStream(buffer, defaultOut);
      }
    ...

    ByteArrayOutputStream changeSystemOutputStream(){
      ByteArrayOutputStream  buffer = new ByteArrayOutputStream();
      System.setOut(new PrintStream(buffer));
      return buffer;
    }

    void revertSystemOutputStream(ByteArrayOutputStream buffer, PrintStream defaultOut ) {
      System.setOut(defaultOut);
      writeToFile(buffer.toString());
    }