Friday, April 10, 2015

Logging in Java


Logging can be defined as a way to record or see desired messages in some form during the execution of code. It help us in seeing and understanding the execution flow of of our programs or application.At some point of time every serious code base have it.
There are lot of ways in which logging can be achieved in java.We have various  readymade framework which we can use for logging.
Since there are so many options available , some time folks get confused what is what..So i thought of putting some clarity ..
we  mostly use  following logging frameworks - log4j, commons-logging ,java logging ,slf4, avlon logging , and i guess they are most popular too..
As a matter of fact, Sun java itself provides logging package.
It was introduced in jdk 1.4. we have java.util.logging package.
very simple usage
Logger logger = Logger.getLogger("xvz.com");
logger.info("This is java Util logging");
Thats it!!..if you run above piece of code (obviously putting it in main method) it will print "This is java Util logging" on the console.
There are many configuration parameters or properties which can be set in 2 ways -
1. Through java code.
2. Through property  file.(Either we can have $java_home/lib/logging.properties - then we don't have to do any initialization in the code , Or we can have property file with logger configuration - it needs to be initialized through code.)
Most of time we just need to configure just two things - log level and location of the property file
Adding this to above code will help us in achieving this

FileHandler handler = new FileHandler("c:\\drive1\\demo.log");
logger.addHandler(handler);
logger.setLevel(Level.ALL);
handler.setFormatter(new SimpleFormatter())


However it is advisable to put configuration stuff in property file - outside the code base as it help it changing the logging configuration without changing the code base.
Now lets dig bit deeper into logging package understand some main basic concepts..
Log Levels - In almost all logging frameworks we have different log levels and each framework have its own naming conventions for these log levels.Different Log Levels helps us in classifying our log messages (msg text which we want to present in our log file).For Example we can have different log level for error messages and different log level for common messages. Now once our program is up and running in production environment , we probably don't need to log common messages as it will unnecessarily consume system resources , but however we would like to log error messages, as it will help us in figuring out the problem later.So to handle above use case we have different log levels .they indicate how important the log message is and we have a hierarchy of log message ranked as per importance of messages.
We have have following log levels in order of severity or importance.

SEVERE (most important)
WARNING
INFO
CONFIG
FINE
FINER
FINEST (least important)

We can set the log levels only among the above mentioned seven level. Let us see how it works..
If log level is set as info
logger.setLevel(Level.info)
and if we log two statements,,
logger.info("This is info");
logger.finest("This is finest");
then only "this is info" is printed. However if we have log level as finest
logger.setLevel(Level.finest);
Then both "this is info" and "This is finest" is printed. the thing which we need to understand here is that ,whatever log level we set , we will have log statements from that level , as well as the level above it.Lowest the log level , we will have more log statements in our log file.
If logger is used intelligently , then it becomes very easy to debug and fix the errors.