实际上我正在尝试为我的动态Web项目实现log4j.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.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其实我对log4j很新,所以我不知道如何编写log4j.xml文件以及在哪里放置该文件exaclty.Kindly帮我解决这个问题。谢谢你提前
答案 0 :(得分:1)
使用此链接http://www.mkyong.com/struts2/struts-2-log4j-integration-example/此链接将为您提供有关使用log4j
的完整信息答案 1 :(得分:0)
根据您发布的配置。您没有使用xml但属性文件。正如Adam Dyga所说,只要log4j.properties文件在类路径中,它就会被应用程序自动拾取。您不需要显式加载。要设置日志文件的位置,
log4j.appender.rollingFile.file=D:/MyWEBLogs/MyWEB.log