導(dǎo)入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
內(nèi)容如下
log4j.rootLogger=INFO, stdout, logfilelog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%nlog4j.appender.logfile=org.apache.log4j.RollingFileAppenderlog4j.appender.logfile.File=${user.home}/myapplication.loglog4j.appender.logfile.MaxFileSize=512KBlog4j.appender.logfile.layout=org.apache.log4j.PatternLayoutlog4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n可根據(jù)不同的需求進行不同的配置。
在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文件的內(nèi)容            String path = SystemController.class.getClassLoader().getResource("").toURI().getPath();            InputStream in = new BufferedInputStream(new FileInputStream(                    path+"../log4j.properties"));//這個是log4j.properties的文件路徑,根據(jù)自己的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頁面?zhèn)鞯臄?shù)據(jù),設(shè)置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);            //關(guān)閉文件            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的其他配置,一樣按照上面的方法做就行。
新聞熱點
疑難解答