Intercept System.out and prepend date time in Java

倖福魔咒の 提交于 2019-12-12 11:16:54

问题


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.


回答1:


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



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/17247688/intercept-system-out-and-prepend-date-time-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!