Redirect System.out.println
Asked Answered
P

6

35

My application has many System.out.println() statements.

I want to catch messages from println and send them to the standard logger (Log4j, JUL etc).

How to do that ?

Passport answered 12/7, 2010 at 12:20 Comment(2)
possible duplicate of Removing access to System.out in javaEroto
While there are crossovers in the answers for that possible dupe, I don't think it's close enough to merit a match. It's more asking for ways to block output altogether - this one is for capturing it elsewhere.Alcoholicity
A
41

The System class has a setOut and setErr that can be used to change the output stream to, for example, a new PrintStream with a backing File or, in this case, probably another stream which uses your logging subsystem of choice.


Keep in mind you may well get yourself into trouble if you ever configure your logging library to output to standard output or error (of the infinite recursion type, possibly).

If that's the case, you may want to just go and replace your System.out.print-type statements with real logging calls.

Alcoholicity answered 12/7, 2010 at 12:25 Comment(2)
To make your solution complete, I suggest you add instruction on how to revert to the normal standard output once one has intercepted everything required, ie System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));Reenter
@VicSeedoubleyew wouldn't it be better to store the original PrintStream and restore it afterward -- if something else is also redirecting System.out, this won't necessarily restore it correctly.Evaporite
T
36

I had a similar need once. I needed to intercept the output of some 3rd party component and react on a error message. The concept looks like this:

private class Interceptor extends PrintStream
{
    public Interceptor(OutputStream out)
    {
        super(out, true);
    }
    @Override
    public void print(String s)
    {//do what ever you like
        super.print(s);
    }
}
public static void main(String[] args)
{
    PrintStream origOut = System.out;
    PrintStream interceptor = new Interceptor(origOut);
    System.setOut(interceptor);// just add the interceptor
}
Turcotte answered 13/7, 2010 at 5:40 Comment(1)
You may need to override the println(String s) method as well.Insensate
P
9

The better solution is to go through and change all the println statements to use a proper logging library. What you're trying to do is a big hack.

Pericynthion answered 12/7, 2010 at 12:27 Comment(4)
Maybe he is trying to redirect System.out to a Logger ;-)Douzepers
+1: the whole point of a logging framework is to be able to abstract where you are recording the output, you should call its logging methods as your "definitive" output from the code. Not to mention, redirecting System.out to call the logger appears to work; right up to the point where one day you configure the logger to output to stdout, and bam - stack overflow.Cordelia
And this is not that difficult. +1Ziguard
I generally agree, but as mentioned in my answer above, I needed to intercept a 3rd party System.out message. No other way. Therefore, there is a use case for this. There is even a use case for GOTO :-)Turcotte
R
6

Here is how to capture prints to System.out, and then put things back in order :

// Start capturing
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
System.setOut(new PrintStream(buffer));

// Run what is supposed to output something
...

// Stop capturing
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));

// Use captured content
String content = buffer.toString();
buffer.reset();
Reenter answered 11/4, 2016 at 19:54 Comment(0)
Y
3

extending PrintStream is a bad solution as you will have to override all print() and println() methods. Instead, you can capture the stream:

public class ConsoleInterceptor {

    public interface Block {
        void call() throws Exception;
    }

    public static String copyOut(Block block) throws Exception {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        PrintStream printStream = new PrintStream(bos, true);
        PrintStream oldStream = System.out;
        System.setOut(printStream);
        try {
            block.call();
        }
        finally {
            System.setOut(oldStream);
        }
        return bos.toString();
    }
}

Now you can capture it like this:

   String result = ConsoleInterceptor.copyOut(() ->{
        System.out.print("hello world");
        System.out.print('!');
        System.out.println();
        System.out.println("foobar");
    });
    assertEquals("hello world!\nfoobar\n", result);
Yakut answered 25/8, 2017 at 7:8 Comment(1)
Nice reusable solution. Also I think the Block interface could be replaced with Runnable.Lagting
S
-1

I applied the base idea of this and it workes fine. No need to change all the System.out.### and System.err.### stuff.

import java.io.OutputStream;
import java.io.PrintStream;   

public class Interceptor extends PrintStream
{
  /** the logger */
  private Logger log;
  /** the origin output stream */
  PrintStream orig;

  /**
   * Initializes a new instance of the class Interceptor.
   *
   * @param out the output stream to be assigned
   * @param log the logger
   */
  public Interceptor( OutputStream out, Logger log )
  {
    super( out, true );
    this.log = log;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected void finalize() throws Throwable
  {
    detachOut();
    super.finalize();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void print( String s )
  {
    //do what ever you like
    orig.print( s );
    log.logO( s, true );
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void println( String s )
  {
    print( s + Defines.LF_GEN );
  }

  /**
   * Attaches System.out to interceptor.
   */
  public void attachOut()
  {
    orig  = System.out;
    System.setOut( this );
  }

  /**
   * Attaches System.err to interceptor.
   */
  public void attachErr()
  {
    orig = System.err;
    System.setErr( this );
  }

  /**
   * Detaches System.out.
   */
  public void detachOut()
  {
    if( null != orig )
    {
      System.setOut( orig );
    }
  }

  /**
   * Detaches System.err.
   */
  public void detachErr()
  {
    if( null != orig )
    {
      System.setErr( orig );
    }
  }
}


public class InterceptionManager
{
  /** out */
  private Interceptor out;

  /** err */
  private Interceptor err;

  /** log  */
  private Logger log;

  /**
   * Initializes a new instance of the class InterceptionManager.
   *
   * @param logFileName the log file name
   * @param append the append flag
   */
  public InterceptionManager( String logFileName, boolean append )
  {
    log = new Logger();
    log.setLogFile( logFileName, append );
    this.out = new Interceptor( System.out, log );
    this.out.attachOut();
    this.err = new Interceptor( System.err, log );
    this.err.attachErr();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected void finalize() throws Throwable
  {
    if( null != log )
    {
      log.closeLogFile();
    }
    super.finalize();
  }
}

This view lines will enable logging without further code changes:

  if( writeLog )
  {
    logFileName = this.getClassName() + "_Log.txt";
    icMan = new InterceptionManager( logFileName, false );
    System.out.format( "Logging to '%s'\n", logFileName );
  }
Square answered 4/7, 2013 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.