package com.hundsun.log; import java.io.*; import java.lang.reflect.Field; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; public class LogAnalysis { public static String ROOT_FLAG = "<root>\n"; public static String ROOT_END_FLAG = "</root>\n"; public static Pattern orderPattern = Pattern.compile("<message type=\"order_notification\".*?</message>"); public static Pattern tradePattern = Pattern.compile("<message type=\"trade_notification\".*?</message>"); public static HashMap<String, Pattern> patternMap = new HashMap<String, Pattern>() {{ put("order_notification", Pattern.compile("(<message type=\"order_notification\".*?</message>)")); put("trade_notification", Pattern.compile("(<message type=\"trade_notification\".*?</message>)")); put("exchange_code", Pattern.compile("(<exchange_code>(.*?)</exchange_code>)")); put("ccy", Pattern.compile("(<ccy>(.*?)</ccy>)")); put("order_status", Pattern.compile("(<order_status>(.*?)</order_status>)")); put("order_sub_status", Pattern.compile("(<order_sub_status>(.*?)</order_sub_status>)")); put("last_order_action_code", Pattern.compile("(<last_order_action_code>(.*?)</last_order_action_code>)")); put("cr_action_code", Pattern.compile("(<cr_action_code>(.*?)</cr_action_code>)")); put("price", Pattern.compile("create_time>(<price>([0-9.]*?)</price>)")); put("qty", Pattern.compile("(<qty>([0-9.]*?)</qty>)<outstand_qty")); put("bs_flag", Pattern.compile("(<bs_flag>(.*?)</bs_flag>)")); put("outstand_qty", Pattern.compile("(<outstand_qty>(.*?)</outstand_qty>)")); put("exec_qty", Pattern.compile("(<exec_qty>(.*?)</exec_qty>)")); put("exec_price", Pattern.compile("(<exec_price>(.*?)</exec_price>)")); put("order_no", Pattern.compile("(<order_no>(.*?)</order_no>)")); put("last_history_id", Pattern.compile("(<last_history_id>(.*?)</last_history_id>)")); put("product_code", Pattern.compile("(<product_code>(.*?)</product_code>)")); put("order_type", Pattern.compile("(<order_type>(.*?)</order_type>)")); put("order_validity", Pattern.compile("(<order_validity>(.*?)</order_validity>)")); }}; public static String logFilePath = "C:\\Users\\k0021339\\Desktop\\新建文件夹"; public static String resultFilePath = "C:\\Users\\k0021339\\Desktop\\logAnalysisResults"; public static Map<String, HashSet<String>> logResult = new HashMap<>(); public static List<EntrustInfo> entrustInfos = new ArrayList<>(); public static void main(String[] args) { File resultFile = new File(resultFilePath); if (!resultFile.exists()) { resultFile.mkdir(); } File file = new File(logFilePath); if (file.isDirectory()) { File[] paths = file.listFiles(); for (File logFile : paths ) { deal(logFile); } } writeResultFiles(); writeEntrustInfos(); System.out.println("log文件处理完毕"); } public static void deal(File srcFile) { System.out.println("开始处理文件[" + srcFile.getName() + "]..."); if (!srcFile.exists()) { System.out.println("原文件不存在..."); return; } try (FileReader fileReader = new FileReader(srcFile); BufferedReader bf = new BufferedReader(fileReader)) { analysisLog(fileReader, bf); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("文件[" + srcFile.getName() + "]处理完毕"); } private static void writeResultFiles() { logResult.forEach((key, value) -> { File file = new File(resultFilePath + "\\" + key + ".xml"); file.delete(); if (!file.exists()) { try { file.createNewFile(); } catch (IOException ex) { ex.printStackTrace(); } } try (FileWriter fileWriter = new FileWriter(file.getAbsolutePath(), true)) { fileWriter.append(ROOT_FLAG); value.forEach(p -> { if (p.length() > 0) { try { fileWriter.append(p).append("\r\n"); } catch (IOException e) { e.printStackTrace(); } } }); fileWriter.append(ROOT_END_FLAG); fileWriter.flush(); } catch (IOException ex) { ex.printStackTrace(); } }); } private static void writeEntrustInfos() { File file = new File(resultFilePath + "\\" + "entrustInfos.csv"); file.delete(); if (!file.exists()) { try { file.createNewFile(); } catch (IOException ex) { ex.printStackTrace(); } } Class entrustInfoClass = EntrustInfo.class; Field[] fields = entrustInfoClass.getDeclaredFields(); try (FileWriter fileWriter = new FileWriter(file.getAbsolutePath(), true)) { fileWriter.append(Stream.of(fields).map(p -> p.getName()).collect(Collectors.joining(",")) + "\r\n"); entrustInfos.forEach(p->{ String fieldValues = Stream.of(fields).map(q -> { try { return (String)q.get(p); } catch (IllegalAccessException e) { e.printStackTrace(); } return ""; }).collect(Collectors.joining(",")) + "\r\n"; try { fileWriter.append(fieldValues); } catch (IOException e) { e.printStackTrace(); } }); fileWriter.flush(); } catch (IOException ex) { ex.printStackTrace(); } } private static void analysisLog(FileReader fileReader, BufferedReader bf) { String str; // 按行读取字符串 while (true) { try { if (!((str = bf.readLine()) != null)) break; String finalStr = str; EntrustInfo entrustInfo = new EntrustInfo(); if (finalStr.contains("trade_notification")) { int a = 0; } if (!orderPattern.matcher(finalStr).find() && !tradePattern.matcher(finalStr).find()) { continue; } patternMap.forEach((key, value) -> { HashSet contents = logResult.getOrDefault(key, new HashSet<>()); String fieldValue = getFieldValue(value, finalStr, contents); logResult.putIfAbsent(key, contents); if (key.equals("ccy")) { entrustInfo.ccy = fieldValue; } if (key.equals("exchange_code")) { entrustInfo.exchangeCode = fieldValue; } if (key.equals("order_status")) { entrustInfo.orderStatus = fieldValue; } if (key.equals("order_sub_status")) { entrustInfo.orderSubStatus = fieldValue; } if (key.equals("last_order_action_code")) { entrustInfo.lastOrderActionCode = fieldValue; } if (key.equals("cr_action_code")) { entrustInfo.crActionCode = fieldValue; } if (key.equals("price")) { entrustInfo.price = fieldValue; } if (key.equals("qty")) { entrustInfo.qty = fieldValue; } if (key.equals("bs_flag")) { entrustInfo.bsFlag = fieldValue; } if (key.equals("outstand_qty")) { entrustInfo.outstandQty = fieldValue; } if (key.equals("exec_qty")) { entrustInfo.execQty = fieldValue; } if (key.equals("exec_price")) { entrustInfo.execPrice = fieldValue; } if (key.equals("order_no")) { entrustInfo.orderNo = fieldValue; } if (key.equals("last_history_id")) { entrustInfo.lastHistoryId = fieldValue; } if (key.equals("product_code")) { entrustInfo.productCode = fieldValue; } if (key.equals("order_type")) { entrustInfo.orderType = fieldValue; } if (key.equals("order_validity")) { entrustInfo.orderValidity = fieldValue; } }); entrustInfos.add(entrustInfo); } catch (IOException ex) { ex.printStackTrace(); continue; } } } private static String getFieldValue(Pattern pattern, String text, HashSet<String> contents) { Matcher m = pattern.matcher(text); String str = ""; String value = ""; if (m.find()) { str = m.group(1); try { value = m.group(2); } catch (Exception e) { } } contents.add(str); return value; } public static class EntrustInfo { public String orderNo; public String lastHistoryId; public String exchangeCode; public String productCode; public String ccy; public String orderType; public String orderValidity; public String bsFlag; public String price; public String qty; public String outstandQty; public String execQty; public String execPrice; public String orderStatus; public String orderSubStatus; public String lastOrderActionCode; public String crActionCode; } }
2020-09-10
最新推荐文章于 2021-07-07 16:24:06 发布