使用Java解析日志文件以仅显示消息

时间:2017-06-27 17:36:17

标签: java parsing logging

我很难让这个工作。我有一个非常大的.log文件,我需要读入代码,我需要解析和格式化代码,然后将其导出到包含正确顺序的正确信息的文本文档。到目前为止,这是我的代码:

import java.util.*;
import java.io.*;

public class chatLogParse {
public static void main(String args[]) throws IOException {

String path = "sampleData.log";
FileInputStream inputStream = null;
Scanner input = null;
String filename;
// Connecting to a file with a buffer
        PrintWriter outFile = new PrintWriter(
                              new BufferedWriter(
                              new FileWriter("ChatOutput.txt")));

try {inputStream = new FileInputStream(path);
input = new Scanner(inputStream, "UTF-8");
while (input.hasNextLine()) {
    String line = input.nextLine();
    System.out.println(line);
}
// note that Scanner suppresses exceptions
if (input.ioException() != null) {
    throw input.ioException();
}
} finally {
if (inputStream != null) {
    inputStream.close();
}
if (input != null) {
    input.close();
    }
   }
  }
 }

以下是日志文件的片段:

08:25:26.668 [D] [T:000FF4] [F:LANTALK2C] <CMD>LANMSG</CMD>
<MBXID>1124</MBXID><MBXTO>5760</MBXTO><SUBTEXT>LanTalk</SUBTEXT><MOBILEADDR>
</MOBILEADDR><LAP>0</LAP><SMS>0</SMS><MSGTEXT>but didn't give me the info I 
needed</MSGTEXT>
08:25:26.672 [+] [T:000FF4] [S:1:1:1124:5607:5] LANMSG [0/2 | 0]
08:25:26.872 [+] [T:000FE8] [S:1:1:1124:5607:5] LANMSGTYPESTOPPED [0/2 | 0]
08:25:28.084 [+] [T:000F1C] [O:TASK] <<< Processing: 0
08:25:29.526 [+] [T:000FEC] [S:1:1:1124:5607:5] LANMSGTYPESTARTED [0/2 | 0]
08:25:31.101 [+] [T:000F1C] [O:TASK] <<< Processing: 0
08:25:34.119 [+] [T:000F1C] [O:TASK] <<< Processing: 0
08:25:34.600 [D] [T:000FF8] [F:LANTALK2C] <CMD>LANMSG</CMD>
<MBXID>1162</MBXID><MBXTO>5607</MBXTO><SUBTEXT>LanTalk</SUBTEXT><MOBILEADDR>
</MOBILEADDR><LAP>0</LAP><SMS>0</SMS><MSGTEXT>i have no clue whats going 
on</MSGTEXT>
08:25:34.610 [+] [T:000FF8] [S:1:1:1162:5760:5] LANMSG [15/2 | 0]
08:25:34.800 [+] [T:000FF0] [S:1:1:1162:5760:5] LANMSGTYPESTOPPED [0/2 | 0]

以下是我的输出格式:

8:25:00 AM [John Smith] to [Steve Jobs] but didn't give me the info I needed
8:25:00 AM [John Smith] to [Steve Jobs] i have no clue whats going on

我需要代码来排序代码行,只选择包含LANTALK的行,按时间顺序应用格式和输出,同时丢弃所有其他行。我不知道该怎么做。任何你能给我的帮助将不胜感激!谢谢!

0 个答案:

没有答案