log4j将日志输出到相对目录
很简单,我喜欢将Log跟着webRoot走,这样随便你项目移到哪,都可以直接运行,不要再运行这运行那。
?
思路是写一个初始化log4j的servlet。
?
?
public class Log4jInit extends HttpServlet { static Logger logger = Logger.getLogger(Log4jInit.class); public Log4jInit() { } public void init(ServletConfig config) throws ServletException { String prefix = config.getServletContext().getRealPath("/"); String file = config.getInitParameter("log4j"); String filePath = prefix + file; Properties props = new Properties(); try { FileInputStream istream = new FileInputStream(filePath); props.load(istream); istream.close(); //toPrint(props.getProperty("log4j.appender.file.File")); String logFile = prefix + props.getProperty("log4j.appender.file.File");//设置路径 props.setProperty("log4j.appender.file.File",logFile); PropertyConfigurator.configure(props);//装入log4j配置信息 } catch (IOException e) { toPrint("Could not read configuration file [" + filePath + "]."); toPrint("Ignoring configuration file [" + filePath + "]."); return; } } public static void toPrint(String content) { System.out.println(content); } } ??
?
?
?
refurl:http://sharep.blog.51cto.com/539048/143734
?
?
?
?
?