Posts Tagged ‘SL4j’

Logging in J2EE Applications

Tuesday, June 1st, 2010
Logging is the one of the most important quality a software developer should have as a practice while coding for their applications. Logging serves as many purpose from debugging the application when a developer encounters an issue, trouble shooting the users issue, when it’s in production mode and can be used to fine tune our application with some performance measures. So logging becomes so important in a Web Application. So what are the ways we can add logging to our application which is developed in J2EE platform, there are many ways to do, they are,
  • Using Logging Framework - Log4J, SL4J, Java Logging API and Apache Commons Logging. These logging framework provides lots of functionality built-in. These frameworks defines three different processors for logging. They are Loggers, Formaters and Appenders. And these frameworks provides many levels for logging also. These frameworks provides many means of organizing your logs like Storing into Files, Database tables and some times these framework will help us in sending the email alerts also. There are many open source loggers can be found here.
  • Custom Developed Loggers - This mayn’t be complete logging solution. Sometimes we may need to code our own way of logging, this depends on our requirement to log. For example We may wanted to just to record only the user actions in all of the screens in an application and the admin needs the screen to see all the action done by that particular user with some more criteria like date etc… This time instead me adding one more framework and cluttering the application configuration, we can simply make use of the existing JDBC connection and store all the actions on the same database where we do store our application data. The other scenario i can think off here is, say for example i’m planning to log many details from each and every request of the user, in this case logging becomes heavier to maintain for the containers, this time we can try use our own loggers with pooling of objects on the user basis.
What are the things should be logged?
There are some categories of information which always has to be logged, they are Error(s) and Exception(s). Apart from these login and logout information, SQL queries and their execution times, parameters if we have any for prepared statement(including Stored Procedures), if we have any interfacing application then the request and response information, basically all the domain objects(internal data structures in the application), each and every user actions like button clicks, session time out for the user session also can be logged for better troubleshooting and debugging purposes. But it’s completely developers duty that they log the IP Address for the Client, Browser Agent, TimeStamp and the user id/information are getting captured in the log information this will make a real big impact on the debugging for web applications.
In my previous experience i have come across some logging mechanisms where they’ve used their own logging tools(basically this made me to write about the custom logging frameworks). Little more details about it.
Using JDBC with DB tables - The basic idea here is have a dB table for user history, store the important actions from the user(s) like button clicks, important queries. But we have to make sure that we are making use of the connection pool, otherwise this becomes costly inside our application and the number of connections shoots up always. The advantage of this approach is easy way of accessing the logs, i.e. a screen inside the same application show this log. Admin of the application can look into these details and can identify few issues of the users. The user’s action sequence can be identified easily. The major drawbacks of this approach is when there is an huge data available as history, then the admin page loading time will become more, the only option to have an archive mechanism to solve this issue.
Using File System -  This approach is like Log4j where we’ll make use of files to write our logs and this can be rotated on daily basis. And this can be divided further more saying the application logs and the DB logs can be separated into two different files. And further more this can be used for each and every module in the application can have two files like application log and database logs. But this approach can be used to log each and every action/request from the user with data. And the naming convention can be on the unique user id. Each user session contains the pool of these objects so that the number of I/O operations are not exceeding the limits. This would be very good approach in logging for a web application since each and every request for the server will be logged for that particular user.
The final important thing here is, what ever the method we follow to log the details we should be able to troubleshoot/debug the applications easily/fastly, even it’s going to be in production. The logging is an very important factor that we should be considering when we think of designing/developing an web application.
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Get Adobe Flash player