启动的时候,增加VM参数,-Dlog4jOutputPath=c:/log4j
---------------------------------------------------------------------------------------------------------- 第2 种做法,适合开发和运维都是同一人的情况,把log4j的变量,放在Web.xml里,部署的时候,需要手动修改这个变量。 log4j配置
可在web.xml中配置log4j.xml的位置,参数名称为:log4jXmlPath。也可以,配置log4j的日志输出位置的目录,参数名称为:log4jOutputPath。
注意:这2个参数都必须输入绝对地址,如果log4jXmlPath没有值,或者对应的文件不存在,将把classpath下的log4j.xml作为默认配置文件。如果再找不到,将报错,log4j配置失败。Log4jInit cn.fansunion.common.web.Log4jInit log4jXmlPath C:/log4j.xml log4jOutputPath C:/log4j/xiaolei2 0
如果log4jOutputPath没有值,或者对应的文件不存在,将把classpath下的log4j文件夹作为默认的输出目录。
- Author:
- fansunion@qq.com 2015年1月8日
public class Log4jInit extends HttpServlet { private static final long serialVersionUID = 1L; public void init(ServletConfig config) throws ServletException { // 从web.xml中找到log4j的输出目录 String log4jOutputPath = config.getInitParameter("log4jOutputPath"); // 默认的日志输出位置 if (StringUtils.isBlank(log4jOutputPath)) { log4jOutputPath = Log4jInit.class.getClassLoader().getResource("") .getFile() + "/log4j"; } File log4jOutputPathFile = new File(log4jOutputPath); // 如果输出文件不存在,手动创建 boolean log4xmlFileExists = log4jOutputPathFile.exists(); if (!log4xmlFileExists) { System.out.println(log4jOutputPathFile.mkdirs()); } // log4j.xml文件中的变量是在这里设置的 System.setProperty("log4jOutputPath", log4jOutputPath); // 从web.xml中找到log4j.xml的输出目录 String log4jXmlPath = config.getInitParameter("log4jXmlPath"); boolean exist = false; // 如果在web.xml手动配置,log4jXmlPath应该使用绝对地址,否则,就使用默认的位置和文件名就行 if (StringUtils.isNotBlank(log4jXmlPath)) { File file = new File(log4jXmlPath); exist = file.exists(); } // log4jXmlPath默认位于classpath下log4j.xml if (!exist) { URL resource = Log4jInit.class.getClassLoader().getResource( "log4j.xml"); if (resource != null) { log4jXmlPath = resource.getFile(); } } DOMConfigurator.configure(log4jXmlPath); }}---------------------------------------------------------------------------------------------------------- 从技术方面的学习,加入一起好,遇到Boss,是我人生的大幸。要不然,我很难这么快就了解阿里等大公司的一些实际做法。 我决定扎根武汉,是不太可能去阿里系工作的。
怀揣梦想的我,也不可能委身于阿里。
小雷FansUnion-博学的互联网技术工作者,全栈式多屏开发工程师
2015年1月25日
湖北-武汉-循礼门