Thursday, April 7, 2011

Logging Tips

1) Keep Logging where necessary and mandatory.

  • Some places we may want the user to know what is the status of application, so let him know it by providing the sufficient info.
  • Please dont log the any secret info or un encrypted data which gets rendered in plain text format.
  • Please avoid having logging statements in loops. It consumes more cpu usage. If it is mandate then have a single log statement which conveys full info.
  • You can avoid thelogging in loops by displaying the details after/before the loops.
        Eg:  for (int i=0;i< list.size(); i++)
       {
               ------
               logger.info("i: "+i+", value: "+list.get(i));
        }
  • In the above example we haev a logger which logs all the list entries where it is unnecessary, time consuming process, and in realtime who cares about the list entries and what anyone can do that data.. !!? -- no answer .. ;)
  • Instead of that you can log something like below.
  • Eg:
  • logger.info("We have received "+list.size()+" events from the server and going to process them");
  • Eg:
        final Timer timer = new Timer(); // Starts the timer.
        int publishedCount = 0; // To count the published events
        for (int i=0;i< list.size(); i++)
       {
             ------
             publishedCount++;
       }
        logger.info(publishedCount+" events processed and published successfully, time taken: "+timer.getElapsedTime()+" millis");
  • It is very easy, most informative and gives the best performance.
  • So user can see the number of events received count and can see the processed/published count and time taken.


 2) Try to have a complete full meaning content in a single line, so that your application will be greppable.
 3) Better to inject the key words in your logging content, so even a novice can grep your log.
 4) Avoid using the classname and method names in the logging.
             eg: myLogger.info(CLASS_NAME + METHOD_NAME + " Number of Ci Ids  :" + ciDetails.size());           ;-)
5)

No comments:

Post a Comment