Mar 31, 2010

How to Use Log4j?

A very simple incorporation of log4j is here!If you really want to know as to how to incorporate log4j logging in your application, here are the details. Though this is simple, I see a bunch of questions related to this in every java related forum and hence I thought of posting this blog!

  • Let us have a simple Bean.xml and Bean.class. the xml file has the bean definition and the bean class just prints a string. This is a very simple application written to make you all understand the incorporation of log4j!

  • Just create a Spring Project in eclipse (since am using a bean and bean.xml is being used for configuring the bean). The project hierarchy is as shown below. You also have a log4j.properties file (empty file) in the classpath of this project as shown below:



  • Select the project.Right click and go to properties. Here you can add the required jars. For the implementation of log4j in a spring project, you would need
  1. Spring jars (all the 3.0 version jars)
  2. log4j.jar(latest)
  • Now add the following code to your log4j.properties.


# Set root category for logging error messages
log4j.rootCategory=ERROR,AppAppender

# Debug statements logged by Spring framework.
log4j.category.org.springframework=ERROR, AppAppender

#AppAppender is set to DailyRollingFileAppender and new file rolls every day.
log4j.appender.AppAppender.File=/logs/application.log
log4j.appender.AppAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.AppAppender.DatePattern='.'yyyyMMdd
log4j.appender.AppAppender.layout=org.apache.log4j.PatternLayout



  • Now just run bean.java as a java application. Only errors get printed in the log gile.

If you want to change the level of logging, you can make the warninggs or information to get printed in the log file by changing the log4j.rootCategory to INFO, WARN etc.

  • If you have a directory hewrarchy in your application, you can enable logging levels for each of the package by adding an entry as

<br /><br />log4j.category.org.<your package structure>==ERROR, AppAppender<br /><br />


  • AppAppender is nothing but a file in the logs folder under c: whose layout, date pattern and type are as described in the code above. Here, it is a daily rolling appender. A new file gets created everyday and the old file is stored after being renamed with proper dates.


Spring Explorer View in Eclipse 3.5

I am working with Spring 3.0 and I use Eclipse as my IDE. As a novice in the arena of Spring, I was pretty much curious to make use of STS! Developers who are using Spring are really fortunate to have the STS plug-in in eclipse. That is simple awesome and cool!!

The details of installation and configuration for a simple project is given below in this post. I was working at it yesterday and thought of sharing this to all since what took 4-5 hours for me would just be completed in minutes for you!

  • From Eclipse --> Help --> Install New Software and by making use of the update site SpringSource Update Site for Eclipse 3.5, I was able to configure STS in my eclipse Galilee with ease. Please find the figure below and ensure that the following are checked when you get ahead to the update site for the installation of STS
  • On clicking Next and Finish, you should be able to get through the installation within minutes!
  • After this,Restart Eclipse!
  • Once you do this, right click on your project and click Spring Tools --> add Spring Project Nature as shown below
  • On adding the Spring nature to your web application, your project in eclipse would show up as given below:
  • Further to this, go to Project --> Properties --> spring. I am considering a simple spring application. Enable Project specific settings in this window that comes up
  • In project validators tab, ensure that Spring validators and Bean validators are checked as shown below:
  • In project builders tab, ensure that AOP Reference Model Builder and Spring Bean Meta Data Builder are checked as shown below. Then click ok
  • After this, goto Beans Support under Spring as shown below. Here scan and select the list of configuration xml files that you have in your spring application.
  • Once you do this, goto Project menu --> Check the entry 'Build Automatically'
  • Now select the project in the Package Explorer and then go to Window menu --> view --> Other. Here click on Spring Explorer to see the bean and config file listing as shown below. Right click on the xml file and from the context menu, click @RequestMappings


Awesome!!You would be able to see all the request mappings within your application in a single screen view here!!!Pretty awesome and very useful indeed!!!!!The Spring Explorer view in STS is just too cool and superb!! Hats off to the Spring tool developers who did this great work in the customization of Spring Explorer view in Eclipse!

If you find the information pretty helpful, I would really be happy if you would keep me posted via the comments form displayed under this article! If you had wanted some other information related to the same topic, I would suggest you to drop a note to me using the comments form for that would help me in getting back to you with the details you are in need of!

Mar 30, 2010

Spring Framework Fundamentals: A PPT

Here is a power point presentation for those who are very new to Spring Framework. I prepared it for my use and thought of putting it up in my blog since that would be useful for some one too!!!

The spring fundamentals ppt is available in the following location.

PPT on Spring Basics

Do post in your comments if the spring basics ppt was really useful for you!