Friday, October 12, 2012

Best way to edit the system.out.println messages

Suppose your application is a legacy application and system.out.println() is used everywhere and without any date and formatting, now you want to display the date for each message that consoles...

How can you handle it without changing the existing code ..!!!!!???

Got the clue ?? Yes?? then no need to read the below text .. you close this site and go happily.. ;) :)

You are still with me ???? then read the small trick that helps you to format the console output with out changing the code ...


try {
    System.out.println("This should go to stdout");

    PrintStream original = System.out;
    System.setOut(new PrintStream(new OutputStream() {
                StringBuilder buffer = new StringBuilder();
                public void write(int b) {
                       if ((char)b != '\n'){
                          buffer.append((char)b); }
                       else {
                       logger.info(TODAY_DATE_TIME+b);}
                }
            }));
    System.out.println("This should go to the write method defined abouve!!!");

    System.setOut(original);
    System.out.println("this should go to stdout");
}
catch (Exception e){
    e.printStackTrace();
}
Output of the effect of the above change looks like this: 
[2012-Oct-13 12:28:19] INFO [REDIRECTED] - This should go to the write method defined abouve!!!
[2012-Oct-13 12:28:19] INFO [REDIRECTED] - Starting the Trap Receiver..
[2012-Oct-13 12:29:22] INFO [REDIRECTED] - This should go to the write method defined abouve!!!
[2012-Oct-13 12:29:22] INFO [REDIRECTED] - Starting the Trap Receiver..
12:29:46: t /krishna/RD/Arcsight/log/output.log
[2012-Oct-13 12:28:19] INFO [REDIRECTED] - This should go to the write method defined abouve!!!
[2012-Oct-13 12:28:19] INFO [REDIRECTED] - Starting the Trap Receiver..
[2012-Oct-13 12:29:22] INFO [REDIRECTED] - This should go to the write method defined abouve!!!
[2012-Oct-13 12:29:22] INFO [REDIRECTED] - Starting the Trap Receiver..


[2012-Oct-13 12:34:55] INFO [REDIRECTED] - ****** New Trap Received *******
[2012-Oct-13 12:34:55] INFO [REDIRECTED] - Received at: Sat Oct 13 12:29:22 IST 2012                                                                                             
[2012-Oct-13 12:34:55] INFO [REDIRECTED] - From: 127.0.0.1/51929                                                                                                                 
[2012-Oct-13 12:34:55] INFO [REDIRECTED] - PDU: V1TRAP[reqestID=0,timestamp=0:00:00.00,enterprise=1.3.7.9.45.89,genericTrap=0,specificTrap=0, VBS[1.3.6.1.2.1.1.4.0 = Rajakrishna Reddy]]                                                                                                                                                                         
[2012-Oct-13 12:34:55] INFO [REDIRECTED] - securityName: public                                                                                                                  
[2012-Oct-13 12:34:55] INFO [REDIRECTED] - securityModel: 1                                                                                                                      
[2012-Oct-13 12:34:55] INFO [REDIRECTED] - securityLevel: 1                                                                                                                      
[2012-Oct-13 12:34:55] INFO [REDIRECTED] -                                                                                                                                       
[2012-Oct-13 12:34:55] INFO [REDIRECTED] - Processed 45.45454545454545/s, total=1   
FUN to be coding!!

No comments:

Post a Comment