简体   繁体   中英

How to create a log4j.xml file for the dynamic web project?

Actually I am trying to implement log4j.file for my dynamic web project. Here is my web.xml

Here I have given web.xml code for only log file

web.xml

<!-- Log File-->

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <listener>
    <listener-class>com.ss.logFile.startupListener.StartupListener</listener-class>
  </listener>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/classes/log4j.properties</param-value>
  </context-param>
</web-app>

log4j.properties

log4j.rootLogger=ERROR, stdout, rollingFile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File=WEB-INF/logs/application.log
log4j.appender.rollingFile.MaxFileSize=512KB
log4j.appender.rollingFile.MaxBackupIndex=10
log4j.appender.rollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.rollingFile.Encoding=UTF-8

StartupListener

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
@WebServlet("/StartupListener")
public class StartupListener extends HttpServlet implements ServletContextListener {
    private static final long serialVersionUID = 1L;

    public void init(ServletConfig config) throws ServletException {    
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
    }
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        java.util.logging.Logger logger = null;
        String log4jFile = sce.getServletContext().getInitParameter("log4jFileName");
        DOMConfigurator.configure(sce.getServletContext().getRealPath(log4jFile));
        logger = LogManager.getLogger(StartupListener.class.getName());
        logger.debug("Loaded: " + log4jFile);   
    }
}

Sir Actually I am very new to log4j, so that i don't how to write log4j.xml file and where to place that file exaclty.Kindly help me to solve this issue.Thanks in advance

使用此链接http://www.mkyong.com/struts2/struts-2-log4j-integration-example/此链接将为您提供使用log4j的完整信息

According to the config you posted. You are not using xml but properties file. As Adam Dyga had said as long as the log4j.properties file is in the classpath, it will be picked up by the application automatically. You do not need to load explicitly. To set the location of the log file,

log4j.appender.rollingFile.file=D:/MyWEBLogs/MyWEB.log

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM