Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure the log4j output file path in web.xml and log4j.properties?

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.

like image 942
Thinker Avatar asked Oct 31 '12 07:10

Thinker


People also ask

How do you set a log4j properties file location?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file.


1 Answers

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.

like image 166
GARIMA KILLEDAR Avatar answered Nov 15 '22 22:11

GARIMA KILLEDAR