其中合法的,需要处理的日志行的基本模版如下:
INFO [11:30:09.356] [dw-23 - GET ....../name_cid_check/%E6%9D%8E%E6%9E%97%E5%B3%B0/XXXX] api service v1 - access:G1;数据源名称;127.0.0.1;****|XXXX;{"data":{"desc":"一致","status":"1","cid":"XXXX","name":"****"},"msg":"success","code":0}
结构为:
INFO [---][dw-两位数字 GET restfull url] api service v1 - access:G1;数据源名称;访问ip;被查询姓名|被查询身份证号;json格式返回结果
-
首先完成文件的读写
def readLine(fileName) {
new File(fileName).eachLine {
line ->checkNeed "${line}"
}
}
def checkNeed(lineStr){
println lineStr
}
-
完成文件遍历,调用文件读取
def main() {
def days = [/2016-05-13/, /2016-05-14/, /2016-05-15/, /2016-05-16/, /2016-05-17/, /2016-05-18/,/2016-05-19/, /2016-05-20/, /2016-05-21/, /2016-05-27/, /2016-06-01/, /2016-06-07/,/2016-06-09/, /2016-06-12/, /2016-06-13/, /2016-06-14/]
//遍历数组的方式: 数组.each{成员变量 ->处理语句}
days.each { perday ->
readLine("I:\\待处理\\2016-06-14日志处理\\application-" + perday + "-0.log", perday);
}
}
-
匹配大括号和获取IP的正则表达式
def 匹配大括号(txt) {
def matcher = txt =~ /\[(.*?)\]/
//可以加入任何需要过滤的特殊字符(注意有的需要转义)
def result=[];
while (matcher.find()) {
result.add( matcher.group(1));
}
return result;
}
def getIp(txt){
def matcher = txt =~/(\d+\.\d+\.\d+\.\d+)/;
def result=[];
while (matcher.find()) {
result.add( matcher.group(0));
}
return result;
}
-
开发完毕后,生成一个字符串,另存为csv,然后用excel处理一下即可