要求:
是对外暴露restful接口插入日志数据,每调用一次插入一条数据,数据来源于fmm-.log(见附件),需要开发出相应的解析代码。
基本流程:
1 读取本地文件fmm-qplatform.log
2 解析该文件里对应的表字段内容(请用正则表达式解析)
3 调用restful接口插入这些数据到数据库表中(请用httpclient工具)
片段数据:
2015-05-28 16:14:21,405 [pool-2-thread-1][INFO ] ch.Filter1 - chq中航段为:null
2015-05-28 16:14:21,405 [pool-2-thread-1][INFO ] ch.Filter1 - chq中航班号 为:CZ3452
2015-05-28 16:14:21,405 [pool-2-thread-1][INFO ] ch.Filter1 - chq中开始日期为Thu May 28 00:00:00 CST 2015
2015-05-28 16:14:21,405 [pool-2-thread-1][INFO ] ch.Filter1 - chq中结束日期为Thu May 28 00:00:00 CST 2015
2015-05-28 16:14:21,405 [pool-2-thread-1][INFO ] ch.Filter1 - 根据解析的信息从q转移表里没有找到对应的转移officeNo,qn处理
2015-05-28 16:14:21,405 [pool-2-thread-1][INFO ] sender.EtermSession - sendCommand cmd = QD
2015-05-28 16:14:23,234 [pool-2-thread-1][INFO ] sender.EtermSession - sendCommand result as follows:
2015-05-28 16:14:23,234 [pool-2-thread-1][INFO ] sender.EtermSession - CAN001 S C QUE ( 0350 ) ( 0348 ) 2015-05-28 13:04:47
TIME CHANGE:1910—>1945
SC:T/CZ3452/28MAY/28MAY/4
2015-05-28 16:14:23,266 [pool-2-thread-1][INFO ] parser.CHParser - 当前的屏幕信息解析出来的字段是:
2015-05-28 16:14:23,266 [pool-2-thread-1][INFO ] parser.CHParser - 变更类型:时刻变更
2015-05-28 16:14:23,267 [pool-2-thread-1][INFO ] parser.CHParser - 航班号:CZ3452
2015-05-28 16:14:23,267 [pool-2-thread-1][INFO ] parser.CHParser - 起始日期:20150528
2015-05-28 16:14:23,267 [pool-2-thread-1][INFO ] parser.CHParser - 结束日期:20150528
2015-05-28 16:14:23,267 [pool-2-thread-1][INFO ] parser.CHParser - 周期:4
2015-05-28 16:14:23,267 [pool-2-thread-1][INFO ] parser.CHParser - office号:CAN001
2015-05-28 16:14:23,267 [pool-2-thread-1][INFO ] parser.CHParser - 屏幕:SC:T/CZ3452/28MAY/28MAY/4
2015-05-28 16:14:23,267 [pool-2-thread-1][INFO ] ch.Filter1 - chq中航段为:null
2015-05-28 16:14:23,267 [pool-2-thread-1][INFO ] ch.Filter1 - chq中航班号 为:CZ3452
2015-05-28 16:14:23,267 [pool-2-thread-1][INFO ] ch.Filter1 - chq中开始日期为Thu May 28 00:00:00 CST 2015
2015-05-28 16:14:23,267 [pool-2-thread-1][INFO ] ch.Filter1 - chq中结束日期为Thu May 28 00:00:00 CST 2015
2015-05-28 16:14:23,283 [pool-2-thread-1][INFO ] ch.Filter1 - 根据解析的信息从q转移表里没有找到对应的转移officeNo,qn处理
2015-05-28 16:14:23,283 [pool-2-thread-1][INFO ] sender.EtermSession - sendCommand cmd = QD
2015-05-28 16:14:25,221 [pool-2-thread-1][INFO ] sender.EtermSession - sendCommand result as follows:
2015-05-28 16:14:25,221 [pool-2-thread-1][INFO ] sender.EtermSession - CAN001 S C QUE ( 0350 ) ( 0347 ) 2015-05-28 13:04:52
AIRCRAFT CHANGE
IM T/CZ3486/28MAY15/28MAY15/4/CKGCAN/324
2015-05-28 16:14:25,254 [pool-2-thread-1][INFO ] parser.CHParser - 当前的屏幕信息解析出来的字段是:
2015-05-28 16:14:25,254 [pool-2-thread-1][INFO ] parser.CHParser - 变更类型:机型变更
2015-05-28 16:14:25,255 [pool-2-thread-1][INFO ] parser.CHParser - 航班号:CZ3486
2015-05-28 16:14:25,255 [pool-2-thread-1][INFO ] parser.CHParser - 起始日期:20150528
2015-05-28 16:14:25,255 [pool-2-thread-1][INFO ] parser.CHParser - 结束日期:20150528
2015-05-28 16:14:25,255 [pool-2-thread-1][INFO ] parser.CHParser - 周期:4
2015-05-28 16:14:25,255 [pool-2-thread-1][INFO ] parser.CHParser - 航段:CKGCAN
2015-05-28 16:14:25,255 [pool-2-thread-1][INFO ] parser.CHParser - 机型:324
2015-05-28 16:14:25,255 [pool-2-thread-1][INFO ] parser.CHParser - office号:CAN001
2015-05-28 16:14:25,255 [pool-2-thread-1][INFO ] parser.CHParser - 屏幕:AIRCRAFT CHANGEIM T/CZ3486/28MAY15/28MAY15/4/CKGCAN/324
2015-05-28 16:14:25,255 [pool-2-thread-1][INFO ] ch.Filter1 - chq中航段为:CKGCAN
2015-05-28 16:14:25,255 [pool-2-thread-1][INFO ] ch.Filter1 - chq中航班号 为:CZ3486
2015-05-28 16:14:25,255 [pool-2-thread-1][INFO ] ch.Filter1 - chq中开始日期为Thu May 28 00:00:00 CST 2015
2015-05-28 16:14:25,255 [pool-2-thread-1][INFO ] ch.Filter1 - chq中结束日期为Thu May 28 00:00:00 CST 2015
2015-05-28 16:14:25,270 [pool-2-thread-1][INFO ] ch.Filter1 - 根据解析的信息从q转移表里没有找到对应的转移officeNo,qn处理
2015-05-28 16:14:25,270 [pool-2-thread-1][INFO ] sender.EtermSession - sendCommand cmd = QD
2015-05-28 16:14:27,318 [pool-2-thread-1][INFO ] sender.EtermSession - sendCommand result as follows:
2015-05-28 16:14:27,318 [pool-2-thread-1][INFO ] sender.EtermSession - CAN001 S C QUE ( 0350 ) ( 0346 ) 2015-05-28 13:05:24
TIME CHANGE:1625—>1740
SC:T/CZ3791/28MAY/28MAY/4
下面是简单的实现…
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FileUtils {
public static void main(String[] args) {
//文件存放的路径
String fileName = "C:\\FMM_log\\fmm-qplatform.log";
List<String> result = FileUtils.readFileContent(fileName);
//日志时间
String Log_Time = "";
//线程名
String Thread_Name = "";
//日志级别
String Log_Leval = "";
//类名
String Class_Name = "";
//日志详情
String Log_Info = "";
List<List<String>> lists = new ArrayList<>();
List<String> namesList = null;
if (result != null && result.size() > 0) {
//遍历解析出来的内容
for (int i = 1; i < result.size(); i++) {
String aa = result.get(i);
// System.out.println("====解析一行行的数据=====》" + result.get(i));
//用正则匹配年月日为开头的的数据为一整条数据,然后对该数据进行截取相关字段信息
String[] strs = aa.split("(\\\\[[^\\\\]]*\\\\])");
for (int j = 0, len = strs.length; j < len; j++) {
System.out.println("没有截取前的数据-------" + strs[j]);
String dd = strs[j].toString();
//用正则截取出时间字段 2015-05-28 16:14:25
String regex = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}";
Pattern p = Pattern.compile(regex);
Matcher matcher = p.matcher(dd);
if (matcher.find()) {
Log_Time = matcher.group(0);
System.out.println("时间:" + Log_Time);
}
//2截取线程名 pool-2-thread-1 日志级别 INFO
List<String> list = extractMessageByRegular(dd);
for (int jj = 0; jj < list.size(); jj++) {
Thread_Name = list.get(0);
Log_Leval = list.get(1);
}
System.out.println("线程名" + Thread_Name);
System.out.println("日志级别:" + Log_Leval);
//4类名
String strStart = " ] ";
String strEnd = " - ";
// 找出指定的2个字符在 该字符串里面的 位置
int strStartIndex = dd.indexOf(strStart);
int strEndIndex = dd.indexOf(strEnd);
//判断是否有指定的内容
if (strStartIndex > 0 && strEndIndex > 0) {
// 开始截取
Class_Name = dd.substring(strStartIndex, strEndIndex).substring(strStart.length());
}
System.out.println("截取的类名:" + Class_Name);
//5日志详情 以登录开始截取到最后
String regeqx = "登录.*";
Pattern pq = Pattern.compile(regeqx);
Matcher matcher1 = pq.matcher(dd);
if (matcher1.find()) {
Log_Info = matcher1.group();
}
System.out.println("日志详情:" + Log_Info);
//namesList存入数组中
namesList = Arrays.asList(Log_Time, Thread_Name, Log_Leval, Class_Name, Log_Info);
System.out.println("截取后的提取的数据=====" + namesList);
}
lists.add(namesList);
}
System.out.println("数组/" + lists);
}
}
/**
* 以行为单位读取文件,读取到最后一行
*
* @param filePath
* @return
*/
public static List<String> readFileContent(String filePath) {
BufferedReader reader = null;
List<String> listContent = new ArrayList<>();
//编码格式
String encoding = "gbk";
String strLine = "";
String ss = "";
try {
//输入流
InputStreamReader read = new InputStreamReader(new FileInputStream(filePath), encoding);// 考虑到编码格
reader = new BufferedReader(read);
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
strLine = reader.readLine();
//对文件内容进行解析存入数组
listContent.add(tempString);
line++;
ss += strLine + "\n";
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
//返回读取的数组
return listContent;
}
/**
* 使用正则表达式提取中括号中的内容
*
* @param msg
* @return
*/
public static List<String> extractMessageByRegular(String msg) {
List<String> list = new ArrayList<String>();
Pattern p = Pattern.compile("(\\[[^\\]]*\\])");
Matcher m = p.matcher(msg);
while (m.find()) {
list.add(m.group().substring(1, m.group().length() - 1));
}
return list;
}
}
下面是截取出来的数据 存放在数组中,至于存入数据库就没有写了
时间:2015-05-28 16:14:39
线程名pool-2-thread-1
日志级别:INFO
截取的类名:ch.Filter1
日志详情:登录office:CAN001
截取后的提取的数据=====[2015-05-28 16:14:39, pool-2-thread-1, INFO , ch.Filter1, 登录office:CAN001]