错误日志收集

来源:互联网 发布:java大视频断点续传 编辑:程序博客网 时间:2024/05/20 21:57

1、config.xml配置文件

<?xml version="1.0" encoding="UTF-8"?><info><DataSource1><DataSourceType>oracle</DataSourceType><DataDriver>oracle.jdbc.driver.OracleDriver</DataDriver><DataUser>itapwh</DataUser><DataPassword>123</DataPassword><DataUrl>jdbc:oracle:thin:@localhost:1521:orcl</DataUrl></DataSource1><File><!-- 修改参数需重启 -->
<span style="font-family: Arial, Helvetica, sans-serif;"><serviceErrorListPath>C:/serviceErrorListPath/</serviceErrorListPath></span>
</File></info>

2、Tools中的errorList方法

<span style="white-space:pre"></span>public synchronized static void errorList(Exception e, String userName) {Date date = new Date();DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");// 时间格式1DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 时间格式2Document doc = null;Element root = null;PrintStream ps = null;OutputStream os = null;try {String pzwjlj=Tools.getPath();File pzwj = new File(java.net.URLDecoder.decode(pzwjlj, "GBK"),"config.xml");doc = new SAXReader().read(pzwj);root = doc.getRootElement().element("File");String folderPath = root.element("serviceErrorListPath").getTextTrim();File folder = new File(folderPath);if (!folder.isDirectory()) { // 目录不存在的话建立目录folder.mkdirs();};String filePath = folderPath + File.separator + df1.format(date)+ ".zydl";File file = new File(filePath);os = new FileOutputStream(file, true);ps = new PrintStream(os);ps.println("(" + df2.format(date) + ")|(" + userName + ")");e.printStackTrace(ps);ps.println("--------------------------------------------------------");ps.flush();System.out.println("/*(" + df2.format(date) + ")|(" + userName+ ")出现错误,错误信息已记入日志*/");} catch (Exception e2) {System.out.println("/*注意:严重错误,错误收集功能出错*/");e2.printStackTrace();} finally {try {ps.close();os.close();} catch (Exception e1) {ps = null;os = null;}}}
3、方法调用

通常在catch中调用Tools.errorList(e,userName);即可。

0 0
原创粉丝点击