Home » Java » Intercept System.out and prepend date time in Java

Intercept System.out and prepend date time in Java

Posted by: admin December 28, 2021 Leave a comment

Questions:

is it possible to intercept calls to System.out.print* and System.err.print* (in Java) and prepend a time stamp to them? Don’t worry, we use the usual logging frameworks, but occasionally some sys.out leaks out and it would be nice to know when it happen so we can tie it up to the proper log files.

Answers:

You can do it.

See the docs

Reassigns the “standard” output stream.
First, if there is a security manager, its checkPermission method is called with a RuntimePermission(“setIO”) permission to see if it’s ok to reassign the “standard” output stream.

public class  CustomPrintStream extends PrinterStream{

 //override  print  methods here 
}

System.setOut(new CustomPrintStream());

###

It should be possible.

System.out is a printStream.

you can extend the stream to append the date and time to the print methods and use System.setOut() to set the stream appropriately.

As an afterthought if you want to identify where the print statements are coming from you can use:
Thread.currentThread().getStackTrace()[1].getClassName();

###

You could use Aspect Oriented Programming to achieve this – in particular the AspectJ tool.

The idea is that you define pointcuts that match at points in your code and then write advice that is executed at those points. The AspectJ compiler will then weave in your advice at those points.

So for your problem you would first define a pointcut that picked up every time you called a print method on a PrintStream

pointcut callPrint(PrintStream ps, String s) : 
              call(* java.io.PrintStream.print*(..)) && target(ps) && args(s);

You would then write advice that would go around this call to replace the argument if the PrintStream is System.out (you could do the same with System.err.

void around(PrintStream ps, String s) : callPrint(ps,s) {
  if(ps.equals(System.out)){
    String new_string = ...
    proceed(new_string);
  }
  else proceed(s);
}

You then need to put this all in an aspect and weave it into your code – there are lots of tutorials online of how to do that.