Spring Logging with Log4J

Logging is very important for any application its give us insider information about application about its background and what happens in the application at debug and run time. It helps us understand what is happening inside the engine by recording parameters silently and provides vital information. It helps in providing system related information like health status, debugging and provides critical support during production trouble shooting and unexplained failure.

Understanding for Log4J Logger Tool:-
Log4j logger contains three main components namely logger, appender and layout. Logger takes care of the logging mechanism and deals with level of logging. Log4j provides five standard levels of logging. There are two more special levels given by log4j. Above all these, log4j allows you to create custom levels.

Five standard log4j levels:-1. DEBUG Level – This log4j level helps developer to debug application. Level of message logged will be focused on providing support to a application developer.

2. INFO Level – This log4j level gives the progress and chosen state information. This level will be generally useful for end user. This level is one level higher than DEBUG.

3. WARN Level – This log4j level gives a warning about an unexpected event to the user. The messages coming out of this level may not halt the progress of the system.

4. ERROR Level – This log4j level gives information about a serious error which needs to be addressed and may result in unstable state. This level is one level higher than WARN.

5. FATAL Level – This log4j level is straightforward and you don’t get it quite often. Once you get this level and it indicates application death.

Two special log4j levels :-ALL Level-
This log4j level is used to turn on all levels of logging. Once this is configured and the levels are not considered.

OFF Level-
This log4j level is opposite to ALL level. It turns off all the logging.

This is very easy to use Log4J functionality inside Spring MVC applications. The following example will take you through simple steps to explain the simple integration between Log4J and Spring or Spring MVC.

Step 1: Log4j library(log4j-x.y.z.jar):- Download the Log4j library from the official website,Step 2:log4j.properties:- Create a Log4j properties file (log4j.properties), put it into the project class path.

Define how log4j handling the logged message, in this example, it will redirect all the logged messages into a text file named “C:logging.out“.

Once you are done with creating source and bean configuration files, let us run the application. If everything is fine with your application, this we will get the following message in browser:

Same time if you will check your C: drive then you should find your log file logging.out with various log messages, something as follows:

------
Going to run HelloLoggingController class
Exiting the program
Invoking afterPropertiesSet() on bean with name 'helloWorld'
Rendering view [org.springframework.web.servlet.view.JstlView: name 'helloWorld'; URL [/WEB-INF/views/helloWorld.jsp]] in DispatcherServlet with name 'sdnext'
Added model object 'message' of type [java.lang.String] to request in view with name 'helloWorld'
Added model object 'author' of type [java.lang.String] to request in view with name 'helloWorld'
Forwarding to resource [/WEB-INF/views/helloWorld.jsp] in InternalResourceView 'helloWorld'
Successfully completed request
----

Jakarta Commons Logging (JCL) API
Alternatively you can use Jakarta Commons Logging (JCL) API to generate log in your Spring application. JCL can be downloaded from the http://jakarta.apache.org/commons/logging/. The only file we technically need out of this package is the commons-logging-x.y.z.jar file, which needs to be placed in your class-path similar way as you had put log4j-x.y.z.jar in the above example.
To use the logging functionality you need a org.apache.commons.logging.Logobject and then you can call one of the following methods as per your requirement:

fatal(Object message)

error(Object message)

warn(Object message)

info(Object message)

debug(Object message)

trace(Object message)

Below is the replacement of HelloLoggingController.java which makes use of JCL API: