log4j相对路径
方法一:
在工程src目录下建立一个log4j.properties文件,该文件会自动拷贝到/WEB-INF/classes下,文件内容如下:
### set log levels ###
log4j.rootLogger = debug, stdout, D, E
### 输出到控制台 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c:%L - %m%n
### 输出到日志文件 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = ${WORKDIR}/logs/log.log
log4j.appender.D.Append = true
## 输出DEBUG级别以上的日志
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%t:%r] - [%p] %m%n
### 保存异常信息到单独文件 ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
## 异常日志文件名
log4j.appender.E.File = ${WORKDIR}/logs/error.log
log4j.appender.E.Append = true
## 只输出ERROR级别以上的日志!!!
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%t:%r] - [%p] %m%n
log4j.rootLogger = debug, stdout, D, E
### 输出到控制台 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c:%L - %m%n
### 输出到日志文件 ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = ${WORKDIR}/logs/log.log
log4j.appender.D.Append = true
## 输出DEBUG级别以上的日志
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%t:%r] - [%p] %m%n
### 保存异常信息到单独文件 ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
## 异常日志文件名
log4j.appender.E.File = ${WORKDIR}/logs/error.log
log4j.appender.E.Append = true
## 只输出ERROR级别以上的日志!!!
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%t:%r] - [%p] %m%n
然后写一个初始化启动的Servlet,代码如下:
package servlets;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class Log4jInit extends HttpServlet {
private static final long serialVersionUID = -5543643811008717484L;
public Log4jInit() {
super();
}
public void destroy() {
super.destroy();
}
public void init() throws ServletException {
// 获得工作目录,修改log4j文件的保存路径
String path = this.getServletContext().getRealPath("");
System.out.println(path);
System.setProperty("WORKDIR", path);
super.init();
}
}
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class Log4jInit extends HttpServlet {
private static final long serialVersionUID = -5543643811008717484L;
public Log4jInit() {
super();
}
public void destroy() {
super.destroy();
}
public void init() throws ServletException {
// 获得工作目录,修改log4j文件的保存路径
String path = this.getServletContext().getRealPath("");
System.out.println(path);
System.setProperty("WORKDIR", path);
super.init();
}
}
web.xml配置如下:
这样就可以在代码中直接使用了
<%@ page import="org.apache.log4j.Logger;"%>
<%
Logger log = Logger.getLogger(this.getClass());
log.info("记录信息");
log.debug( " debug " );
log.error( " error " );
%>
方法二:
去掉方法一中配置文件log4j.properties中的${WORKDIR}/
然后修改Log4jInit的init方法如下: