Java Web动态配置log4j

导入log4j的jar包,

在web.xml中做如下配置

  <!-- Log4j Configuration -->

    <context-param>

        <param-name>webAppRootKey</param-name>

        <param-value>myapplication.root</param-value>

    </context-param>

    <context-param>

        <param-name>log4jConfigLocation</param-name>

        <param-value>/WEB-INF/log4j.properties</param-value>

    </context-param>

    <context-param>

        <param-name>log4jRefreshInterval</param-name>

        <param-value>60000</param-value><!-- 重新加载log4j配置的间隔时间 ms -->

    </context-param>

在WEB-INF下建立文件log4j.properties

内容如下

log4j.rootLogger=INFO, stdout, logfile



log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n



log4j.appender.logfile=org.apache.log4j.RollingFileAppender

log4j.appender.logfile.File=${user.home}/myapplication.log

log4j.appender.logfile.MaxFileSize=512KB

log4j.appender.logfile.layout=org.apache.log4j.PatternLayout

log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

可根据不同的需求进行不同的配置。

在web页面上做个选择logging level的页面,选择logging level,传入servlet,

  
  //查询logging level,并传到web页面
  @RequestMapping(value="/logging_view", method = RequestMethod.GET) public String toLoggingView(HttpServletRequest request,Model model){ Properties props = new Properties(); try {
       //读取log4j.properties文件的内容 String path
= SystemController.class.getClassLoader().getResource("").toURI().getPath(); InputStream in = new BufferedInputStream(new FileInputStream( path+"../log4j.properties"));//这个是log4j.properties的文件路径,根据自己的servlet的path自行配置 props.load(in); String value = props.getProperty("log4j.rootLogger"); logger.debug("log4j.rootLogger键的值是:"+ value); model.addAttribute("rootLogger", value); in.close(); } catch (URISyntaxException e) { logger.error("SystemController : logging_view : URISyntaxException:"+e); } catch (FileNotFoundException e) { logger.error("SystemController : logging_view : FileNotFoundException:"+e); } catch (IOException e) { logger.error("SystemController : logging_view : IOException:"+e); } return SessionHandler.verifySession(request, "logging_view"); } //获取web页面传的数据,设置logging level @RequestMapping(value="/setLoggingLevel", method = RequestMethod.POST) public String setLoggingLevel(@RequestParam String rootLogger, @RequestParam String loggerLevel, HttpServletRequest request, Model model){ RequestContext requestContext = new RequestContext(request); String[] loggers = rootLogger.split(","); loggers[0] = loggerLevel; String logging = ""; for(int i = 0; i < loggers.length; i++){ logging += loggers[i]+","; } logging = logging.substring(0, logging.length()-1); Properties props = new Properties(); try {
       //将logging level写入log4j.properties文件 String path
= SystemController.class.getClassLoader().getResource("").toURI().getPath()+"../log4j.properties"; InputStream in = new BufferedInputStream(new FileInputStream( path)); props.load(in); OutputStream fos = new FileOutputStream(path); props.setProperty("log4j.rootLogger", logging); props.store(fos,"last update"); String value = props.getProperty("log4j.rootLogger"); logger.debug(value); //关闭文件 in.close(); fos.close(); List<String> list = new ArrayList<String>(); list.add(requestContext.getMessage("logging.level")); if(value.split(",")[0].equals(loggerLevel)){ model.addAttribute("msg", requestContext.getMessage("edit.success",list)); }else{ model.addAttribute("msg", requestContext.getMessage("edit.failed",list)); } } catch (URISyntaxException e) { logger.error("SystemController : logging_view : URISyntaxException:"+e); } catch (FileNotFoundException e) { logger.error("SystemController : logging_view : FileNotFoundException:"+e); } catch (IOException e) { logger.error("SystemController : logging_view : IOException:"+e); } return "msg"; }

这样就完成了,log4j的其他配置,一样按照上面的方法做就行。

你可能感兴趣的:(Java Web)