Development
and Debug are two most important phase in Software developers life. Whenever we
talk about debugging we come across so many tools, best practices, coding styles
etc. Logging is one of the most popular and mandatory concept of debugging the
application. I will say that this is one of the most crucial elements for monitoring
and analyzing the application. Using proper logging mechanism, we can not only
find out the description about errors but also could take certain decisions
for improving system performance. It helps in identifying the issue and
their cause. Sometimes we can also generate metrics based on current logs to
set the target for future road map for the application.
Types of Logging:
Use Log for Debug - To know
the issue, for recording request and responses, for knowing system performance
and many more. Logs could contain the end to end session for an
user.
Use Log for Knowing Stats - You can log for counting number of visitor came to the site, for
voice application, it can be used to know the call details of the user, database
hits and/or anything that could be used for audits etc.
Based on
my work experience and knowledge I would outline some of the best practices
that we should follow for Logging:
#1- Use
categories - Use the severity values like INFO, WARN, ERROR, and DEBUG for each
message.
#2- Proper
Name inclusion - Include the Name of Method,Function,Class etc.
#3- Use Time
information - Timing information and context information can be very useful.
#4- Log the
messages to some Local Files
#5- Try to
log events for all possible resources. For example
Application
logs
DB logs
Any
Network logs
All
Configuration files details
Performance
data details (example - iostat, vmstat, ps, etc.)
Any other
time component
#6- Use user
friendly messages
#7- Use time-stamps
for every log messages
#8- For DB debugging,
use some log table to store the activity from DB procedure, functions etc. It
will help to determine if everything worked well during DB processing. Some
Autonomous function/procedure for logging will also be good to design to have.
#9- Try to
log the end to end flow for a request. Try not to log unnecessary stuff, for
example some details may be duplicate/redundant.
#10- Do not
use sysout for showing messages in console.
#11- You can
log messages in XML format as well, but you have take some extra care for this.
#12- Log both
errors and behavior of the application.
#13- As it
takes too much space, it is advisable to Keep the time limit for storage of Log
file in local system.
#14- In
case of try-catch, use as follows:
Do not use
the following, it is an anti-pattern:
try
{
toDBOperations();
}
catch (SQLException cnce) {
log("Exception in toDBOperations()");
}
Following
ussae is good to go:
try
{
toDBOperations();
}
catch (SQLException cnce) {
log("Exception in toDBOperations()");
rollbackDBData();
}
Hope you
enjoyed reading the blog!!!
Comments
Post a Comment