Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intercept System.out and prepend date time in Java

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.

like image 377
Ben Smith Avatar asked Jun 22 '13 05:06

Ben Smith


3 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 PrintStream{

 //override  print  methods here 
}

System.setOut(new CustomPrintStream());
like image 54
Suresh Atta Avatar answered Nov 06 '22 08:11

Suresh Atta


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();

like image 2
Osama Javed Avatar answered Nov 06 '22 09:11

Osama Javed


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.

like image 2
selig Avatar answered Nov 06 '22 09:11

selig