# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logging.out
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Root logger option
log4j.rootLogger=debug, file, stdout

package com.dineshonjava.controller;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* @author Dinesh Rajput
*
*/
@Controller
public class HelloLoggingController {
//get log4j handler
private static final Logger logger = Logger.getLogger(HelloLoggingController.class);
@RequestMapping("/hello")
public ModelAndView sayLogiingHello(){
//log it via log4j
if(logger.isDebugEnabled()){
logger.debug("Start debug");
}
logger.info("Going to run HelloLoggingController class");
Map model = new HashMap();
model.put("message", "Hello world Example with Logging");
model.put("author", "Dinesh on Java");
logger.info("Exiting the program");
return new ModelAndView("helloWorld", model);
}
}
<beans xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.dineshonjava.controller">
</context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="jspViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
<web-app version="2.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>sdnext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name><param-value>/WEB-INF/config/sdnext-servlet.xml</param-value></init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sdnext</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
Welcome message : ${message}

------ 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 ----
package com.dineshonjava.controller;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* @author Dinesh Rajput
*
*/
@Controller
public class HelloLoggingController {
static Log log = LogFactory.getLog(HelloLoggingController.class.getName());
@RequestMapping("/hello")
public ModelAndView sayLogiingHello(){
//log it via log4j
if(logger.isDebugEnabled()){
logger.debug("Start debug");
}
logger.info("Going to run HelloLoggingController class");
Map model = new HashMap();
model.put("message", "Hello world Example with Logging");
model.put("author", "Dinesh on Java");
logger.info("Exiting the program");
return new ModelAndView("helloWorld", model);
}
}
You have make sure that you included commons-logging-x.y.z.jar file in your project before deploying the program.Labels: Spring3.0