首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

log4j将日记输出到相对目录

2012-12-28 
log4j将日志输出到相对目录很简单,我喜欢将Log跟着webRoot走,这样随便你项目移到哪,都可以直接运行,不要再

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

?

?

?

?

?

热点排行