<listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param>
此時,log4j.properties在根目錄src下。
log4j.properties文件
#the first parameter is the level of the log output
#the second parameter is the location of the log output
log4j.rootLogger=debug,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
#use simple layout
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=d:/bjsxt.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %F %p %m%n
在web項目中:一般我們直接將log4j.properties放置在src目錄下,這樣系統自動會找到的,其實就是放在WEB-INF/classes文件下。這個路徑在classpath下,所以直接就能找到。我們寫Logger的時候如下
如果現在我們想把log4j.properties文件放置在其它目錄下,例如:WEB-INF下和web.xml放在一起。這時候就需要我們手動指定log4j配置文件的路徑,否則系統是找不到的。
一、首先我們在web.xml中配置好log4j.properties路徑:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
二、然后寫個servlet,部分代碼如下:
public void init() {
String prefix = getServletContext().getRealPath("/");
String file = getInitParameter("log4jConfigLocation");
if (file != null) {
PropertyConfigurator.configure(prefix + file);
}
}
三、在web.xml中配置servlet,並將log4jConfigLocation加入到Servlet中,讓其Server啟動即運行:
<servlet>
<servlet-name>your servlet</servlet-name>
<servlet-class>your servelt class</servlet-class>
<init-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
轉載自:https://blog.csdn.net/u014756827/article/details/52488586
附:logback的使用與詳解:https://www.cnblogs.com/warking/p/5710303.html
maven配置logback : https://www.cnblogs.com/quchunhui/p/5783172.html
