Log parser to retrive xml message
How to save the desired data out of the log.
package com.qa.pt.conf; import jdk.internal.org.xml.sax.InputSource; import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.DirectoryFileFilter; import org.apache.commons.io.filefilter.WildcardFileFilter; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.*; import java.io.*; import java.text.SimpleDateFormat; import java.util.*; import java.util.regex.Pattern; public class LogParser_GTMOutBound { private static String Folder_path ="C:\\s2hs\\Projects\\Performance\\Conf\\Logs\\"; // this is for 569 private static String Log_Name="adaptor.log_prod"; private static int i =1; private static int j =1; private static int k =1; private static Hashtable<String,String > Counter =new Hashtable<>(); public static void main(String args[]) { Scanner scan = null; try { scan = new Scanner(new File(Folder_path+Log_Name)); } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println("Split whole file"); scan.useDelimiter(Pattern.compile("\n2018")); System.out.println("Split Complete"); while (scan.hasNext()) { String logicalLine= new String(); logicalLine=scan.next() ; logicalLine=logicalLine.replaceAll("\r","").replaceAll("\n",""); if (logicalLine.contains("INFO")) { if (logicalLine.contains("Wrong")) { } else { String[] Customized_LineArray = logicalLine.split("is published"); Hashtable<String, String> details = new Hashtable<String, String>(); details.put("ArgonTime", logicalLine.substring(0, 15)); details.put("MessageType", logicalLine.split("] ")[1].split(" ")[0]); // Split Logicalline second array which contains toatl line and then take the first part of split by space. String[] FinalMessageLine= Customized_LineArray[0].split("Message:"); // Split the EMML containg extra chars details.put("Message", FinalMessageLine[1]); //FinalMessageLine[1] is the Final EMML Message Saved after spliting try { XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Date currdt1 =new Date(); Document document = db.parse(new StringBufferInputStream(details.get("Message"))); String status = xpath.evaluate("/emml/processingEvent/endStateIdentifier/stateId", document); String tradeID = xpath.evaluate("emml/processingEvent/tradeIdentifier[systemDomainName=\"SETTLEMENT_REF\"]/tradeId", document); String TLMtime = xpath.evaluate("/emml/messageHeader/eventDateTime", document); if (status.equals("ADDNOTE") ) { System.out.println("No."+i+" argontime:2018" + details.get("ArgonTime") + " Messagetype:" + details.get("MessageType")+" Status:"+status+ " TradeID:" +tradeID+" TLMsenttime:"+ TLMtime); } //System.out.println("No."+i+" Messagetype:" + details.get("MessageType")+" Status:"+status+ " TradeID:" +tradeID); //the main line Counter.put(details.get("MessageType")+"-"+status,String.valueOf(Integer.parseInt(Counter.getOrDefault(details.get("MessageType")+"-"+status,"0"))+1)); i = i + 1; // counter for correctparsedMessages } catch (XPathExpressionException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { System.out.println("insideException "+details.get("Message")); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } else { //TODO error Counter k=k+1; // counter for error messages } j=j+1; //counter for total messages } System.out.println(Counter.toString()); System.out.println("TotalMessages"+j+ " CorrectMessagesProcessed:"+i +" Errors: "+k); } }
Comments
Post a Comment