使用步骤注意:
1. 从http://logging.apache.org/log4j/1.2/ 下载文件
2. 在src目录下加入log4j.properties,将jar包放入build -path,log4j.properties 编译后位于/classes目录下。
3. 配置文件读取解析:
PropertyConfigurator.configure(String configFilename)---->
PropertyConfigurator.java
staticpublic void configure(String configFilename) { new PropertyConfigurator().doConfigure(configFilename, LogManager.getLoggerRepository()); }
--->读取配置文件
public void doConfigure(String configFileName, LoggerRepository hierarchy) { Properties props = new Properties(); FileInputStream istream = null; try { istream = new FileInputStream(configFileName); props.load(istream); istream.close(); } catch (Exception e) { if (e instanceof InterruptedIOException || e instanceof InterruptedException) { Thread.currentThread().interrupt(); } LogLog.error("Could not read configuration file ["+configFileName+"].", e); LogLog.error("Ignoring configuration file [" + configFileName+"]."); return; } finally { if(istream != null) { try { istream.close(); } catch(InterruptedIOException ignore) { Thread.currentThread().interrupt(); } catch(Throwable ignore) { } } } // If we reach here, then the config file is alright. doConfigure(props, hierarchy); }
-->读取配置
/** Read configuration options from <code>properties</code>. See {@link #doConfigure(String, LoggerRepository)} for the expected format. */ public void doConfigure(Properties properties, LoggerRepository hierarchy) { repository = hierarchy; String value = properties.getProperty(LogLog.DEBUG_KEY); if(value == null) { value = properties.getProperty("log4j.configDebug"); if(value != null) { LogLog.warn("[log4j.configDebug] is deprecated. Use [log4j.debug] instead."); } } if(value != null) { LogLog.setInternalDebugging(OptionConverter.toBoolean(value, true)); } // // if log4j.reset=true then // reset hierarchy String reset = properties.getProperty(RESET_KEY); if (reset != null && OptionConverter.toBoolean(reset, false)) { hierarchy.resetConfiguration(); } String thresholdStr = OptionConverter.findAndSubst(THRESHOLD_PREFIX, properties); if(thresholdStr != null) { hierarchy.setThreshold(OptionConverter.toLevel(thresholdStr, Level.ALL)); LogLog.debug("Hierarchy threshold set to ["+hierarchy.getThreshold()+"]."); } configureRootCategory(properties, hierarchy); configureLoggerFactory(properties); parseCatsAndRenderers(properties, hierarchy); LogLog.debug("Finished configuring."); // We don't want to hold references to appenders preventing their // garbage collection. registry.clear(); }
原文:http://zengjinliang.iteye.com/blog/171550
一:log4j配置文件基本含义说明
}
}
运行一下,看看异常信息是不是保存在了一个单独的文件error.log中。