Logging using Log4j

Logging is an important of any application. There are several APIs available majorly used are:
1. Apache Log4J
2. Java Logging (java.util.logging)

Logging is required to monitor the state of the running application, debug the application, ignorant developers heavily relies on System.out.println(), which is quite expensive in terms of resources and you dont have any way to manage these messages.

To control this we have different API's like mentioned above.

Using Log4J from Apache:
Log4j consists of 3 aspects: logger, appender and, layout relations between three can be expressed as "logger logs to an appender using a layout"

Each class in an application can have seperate logger or a common logger, now this logger should know where to send request for logging. This where are known as appenders which can be:
- FileAppender (flat file)
- JDBCAppender (database )
- Console Appender (Console)
- SMTPAppender (Email)
- JMSAppender (to remote JMS servers)
- SocketAppender (to remote server).
there are few more.

Now we need to decide what and how we want the out out of that log, this is decided by layout

Each class can have a logger as mentioned above similarly each logger should have log level. There are five different log levels (ordered):
- DEBUG
- INFO
- WARN
- ERROR
- FATAL

To configure a class with a logger, appender, layout and log level we need to use an external configuration file (log4j.properties is the default)

Sample configuratiion file is as:
--------------------------------
# Set root logger level to DEBUG and its only appender to CONSOLE.
log4j.rootLogger=INFO, CONSOLE

# A1 is set to be a ConsoleAppender.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# Change the level of messages for various packages.
log4j.logger.com.tj.cand.controller=WARN
log4j.logger.com.tj.cand.domain=WARN
log4j.logger.com.tj.cand.dao=DEBUG
--------------------------------

Usage:

Logger log = Logger.getLogger(MyController.class);
log.info("This is a logging message);




Why Spring Framework

Last year I had to shift my web application to some latest framework.
I decided to go for Spring Framework, EJB3 was also an option.

The reason why Spring was:

1. Spring was a well established Open Source
2. Spring provides all the configurations into XML where as EJB3.0 relies in annotations, in my scenario I am using WebSphere 6.1 which runs on JDK1.4 which doesn't support annotations.

3. Spring is a loosely coupled Framework You can decide your own stack. Whereas EJB is whole integrated package.

4. One major reason was You are not forced to use any Spring imports or rely on Spring classes. Dependency Injection combined with external meta-data helps to achieve this independence. So you can easily migrate. While in case of EJB 3.0 this is different.

5. One disadvantage of using annotations based EJB 3.0 is you need to change java source to incorporate any configurations whereas in case of spring you need to change XML file.