8

I have developed a web application where i can register a employee.

My web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <display-name>Employee Registration</display-name>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
  <servlet-name>sapient</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
  <servlet-name>sapient</servlet-name>
  <url-pattern>*.htm</url-pattern>
  </servlet-mapping>

</web-app>

Now when I click on a Register Button in my html page. It goes to a controller class where I have wrote a code for logging,

@org.springframework.stereotype.Controller
public class RegController  {
    private static final Logger LOGGER = Logger
            .getLogger(RegController.class.getName());


@RequestMapping(value = "/register.htm", method = RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    LOGGER.debug("ENTERING the contoller class");
    ModelAndView mav = new ModelAndView("register");
    LOGGER.debug("exiting the contoller class");
    return mav;

}

}

I have created a package called resource and have created a log4j.properties file in

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\loging.log
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

But my logs are not created in C: drive.

Do I need to configure something in web.xml ? I have already included log4j.jar file.

Lucky
  • 16,787
  • 19
  • 117
  • 151
Thinker
  • 6,820
  • 9
  • 27
  • 54

4 Answers4

9

You need to include-

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/resource/log4j.properties</param-value>
    </context-param>

     <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

in your web.xml. then it will work fine.

GARIMA KILLEDAR
  • 141
  • 1
  • 1
  • and under the /WEB-INF/resource/ add the file log4j.properties with this code `# Root logger option log4j.rootLogger=DEBUG, file # Redirect log messages to a log file log4j.appender.file=org.apache.log4j.RollingFileAppender #outputs to Tomcat home log4j.appender.file.File=${catalina.home}/logs/myapp.log log4j.appender.file.MaxFileSize=5MB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n` – BERGUIGA Mohamed Amine Oct 14 '14 at 08:49
  • Works like a charm. Thanks – Abhiroop Sarkar Oct 04 '15 at 23:42
2

Your question have nothing to do with Spring MVC. It is simply a Log4J configuration issue. (Please edit your question)

Have you setup the logger correctly? (e.g. to allow Debug level, and set your file appender to the logger)

Have something like this in your config (I just recall the syntax by memory, fix it yourself if there is anything wrong)

log4j.rootLogger.level=INFO, file
log4j.logger.your.package.RegController=DEBUG

something like that.

Also, make sure that you don't have other log4j.properties or log4j.xml in classpath. Log4J will probably load them instead of yours, which caused the problem.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • something off-topic: I will strongly suggest OP to adopt SLF4J. You may still use Log4J as the backend for logging but it ease the configuration, and you can change your logging backend in the future – Adrian Shum Nov 01 '12 at 01:14
1

Include following log4j.properties file in resources folder.

log4j.rootCategory=INFO,S,rollingFile

log4j.appender.S =org.apache.log4j.ConsoleAppender
log4j.appender.S.layout =org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n

log4j.appender.rollingFile = org.apache.log4j.DailyRollingFileAppender

#provide path to your location where you want logs created. For now its logs folder of   tomcat.
log4j.appender.rollingFile.File = ${catalina.home}/logs/loging.log
log4j.appender.rollingFile.Append = true
log4j.appender.rollingFile.MaxFileSize=2000KB 
log4j.appender.rollingFile.MaxBackupIndex=9 

log4j.appender.rollingFile.Threshold = ALL

log4j.appender.rollingFile.DatePattern = '.'yyy-MM-dd
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n

Replace your code with this one. Definitely it will work fine.

Jeevan Patil
  • 6,029
  • 3
  • 33
  • 50
0

in your web.xml add this code

   <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/resource/log4j.properties</param-value>
    </context-param>

     <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

and under the /WEB-INF/resource/ add the file log4j.properties with this code

 # Root logger option 
   log4j.rootLogger=DEBUG, file # Redirect log messages to a log file
   log4j.appender.file=org.apache.log4j.RollingFileAppender #outputs to
   Tomcat home log4j.appender.file.File=${catalina.home}/logs/myapp.log
   log4j.appender.file.MaxFileSize=5MB
   log4j.appender.file.MaxBackupIndex=10
   log4j.appender.file.layout=org.apache.log4j.PatternLayout
   log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}
   %-5p %c{1}:%L - %m%n

and in your Class add this code

private static final Logger logger = Logger.getLogger(NameOfYourrClass.class);

and inside the your function add this code

logger.debug("This is Error message", new Exception("Testing"));
BERGUIGA Mohamed Amine
  • 6,094
  • 3
  • 40
  • 38