简体   繁体   English

如何为动态Web项目创建log4j.xml文件?

[英]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. 实际上,我正在尝试为我的动态Web项目实现log4j.file。 Here is my web.xml 这是我的web.xml

Here I have given web.xml code for only log file 在这里,我仅给出了日志文件的web.xml代码

web.xml 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.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 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 先生,实际上我对log4j还是很陌生,所以我不怎么写log4j.xml文件以及将文件放到哪里,请帮助我解决这个问题。

使用此链接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. 您不使用xml,而是使用属性文件。 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. 正如Adam Dyga所说的,只要log4j.properties文件位于类路径中,它将由应用程序自动拾取。 You do not need to load explicitly. 您无需显式加载。 To set the location of the log file, 要设置日志文件的位置,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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