国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Java Web動態(tài)配置log4j

2019-11-14 21:06:38
字體:
供稿:網(wǎng)友
java Web動態(tài)配置log4j

導(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的其他配置,一樣按照上面的方法做就行。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 绿春县| 江西省| SHOW| 都昌县| 项城市| 瑞丽市| 河南省| 齐河县| 彩票| 武功县| 南平市| 广灵县| 连云港市| 德江县| 八宿县| 耒阳市| 石泉县| 汤原县| 上杭县| 留坝县| 荣昌县| 原阳县| 界首市| 广灵县| 广水市| 玉山县| 读书| 临清市| 石阡县| 南木林县| 凤翔县| 高碑店市| 长沙县| 隆安县| 平果县| 濉溪县| 张家口市| 成都市| 铅山县| 沙雅县| 商水县|