在JAR包中使用log4j

前提概要:

一个小系统,需要打成jar包的,而且在这个系统里面需要用到log4j,那么怎么能在打成后的jar包中正确加载到log4j配置文件呢?

实现代码如下(不管是Eclipse环境还是jar包环境都能正确读取到log4j的配置文件):

InputStream in = LogManager.class.getResourceAsStream("/com/test/log4j.xml");
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();  
Document doc = db.parse(in);
DOMConfigurator.configure(doc.getDocumentElement());

我的测试工程目录结构如下:


转载请注明出处.

你可能感兴趣的:(在JAR包中使用log4j)