private static final String LOG_CONFIG_FILE = "/log4j.xml";
static { getLog4jProperties(); }
Properties props = new Properties(); try { String driver = null; String url = null; String username = null; String password = null; InputStream inputStream = new FileInputStream(LOG_CONFIG_FILE); if (LOG_CONFIG_FILE.endsWith(".xml")) { // 载入XML格式的配置文件 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream); DOMConfigurator.configure(doc.getDocumentElement()); } else { // 载入properties格式的配置文件 props.load(inputStream); PropertyConfigurator.configure(props); // 属性配置 } Set s = props.keySet(); Iterator it = s.iterator(); while (it.hasNext()) { String id = (String) it.next(); String value = props.getProperty(id); if (id.equals("log4j.appender.db.driver")) { // 驱动器 driver = value; } else if (id.equals("log4j.appender.db.URL")) { // 数据库地址 url = value; } else if (id.equals("log4j.appender.db.username")) { // 用户名称 username = value; } else if (id.equals("log4j.appender.db.password")) { // 用户密码 password = value; } } } catch (Exception e) { e.printStackTrace(); } }