導入log4j的jar包,web
在web.xml中作以下配置apache
<!-- 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.propertiesapp
內容以下spa
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
可根據不一樣的需求進行不一樣的配置。debug
在web頁面上作個選擇logging level的頁面,選擇logging level,傳入servlet,code
//查詢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的其餘配置,同樣按照上面的方法作就行。xml