log4jxml配置实现.docx

上传人:小飞机 文档编号:3160711 上传时间:2023-03-11 格式:DOCX 页数:6 大小:37.66KB
返回 下载 相关 举报
log4jxml配置实现.docx_第1页
第1页 / 共6页
log4jxml配置实现.docx_第2页
第2页 / 共6页
log4jxml配置实现.docx_第3页
第3页 / 共6页
log4jxml配置实现.docx_第4页
第4页 / 共6页
log4jxml配置实现.docx_第5页
第5页 / 共6页
亲,该文档总共6页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《log4jxml配置实现.docx》由会员分享,可在线阅读,更多相关《log4jxml配置实现.docx(6页珍藏版)》请在三一办公上搜索。

1、log4jxml配置实现log4j.xml配置实现 先写一个Servlet的listener,用来监听服务启动,并自动解析log4j.xml文件 代码如下: import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.apache.log4j.LogManager; import org.apache.log4j.PropertyConfigurator; import org.apache.log4j.xml.DOMConfigurator; public

2、class Log4jConfigListener implements ServletContextListener public static final String CONFIG_LOCATION_PARAM = log4jConfigLocation; public static final String XML_FILE_EXTENSION = .xml; public void contextDestroyed(ServletContextEvent event) / TODO Auto-generated method stub LogManager.shutdown; pub

3、lic void contextInitialized(ServletContextEvent event) / TODO Auto-generated method stub String location = event.getServletContext.getInitParameter(CONFIG_LOCATION_PARAM); if (location != null) if (!location.startsWith(/) location = / + location; location = event.getServletContext.getRealPath(locati

4、on); /如果是xml结尾就用DOM解析,否则就用properties解析 if (location.toLowerCase.endsWith(XML_FILE_EXTENSION) DOMConfigurator.configure(location); else PropertyConfigurator.configure(location); 再写个util类,用来生成所要的不同类型的日志 代码如下: package com.zhaopin.util; import org.apache.log4j.Logger; public class Log4jUtil public stati

5、c Logger getSQLLogger return Logger.getLogger(sql); public static Logger getBusinessLogger return Logger.getLogger(business); public static Logger getSimpleErrorLogger return Logger.getLogger(simpleError); public static Logger getNormalErrorLogger return Logger.getLogger(normalError); 下面开始写配置文件,先要在web.xml下添加信息: log4jConfigLocation /WEB-INF/Log4j.xml Log4jConfigListener 最重要的log4j.xml的配置信息如下: - - - 在程序中应用时需要编写如下代码: Log4jUtil.getBusinessLogger.info(message!);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号