我正在尝试使用java中的正则表达式解析日志。我正在使用java中的日志文件,因此我可以提取日志字段。例如,我有以下一行:
enterprises.140.625.100.50=[STUCK] ExecuteThread: '0' for queue: 'weblogic.kernel.Default<self-tuning>
我希望输出如下:
"enterprises" = Apr 10 21:08:55
"Ip address" = 140.625.100.50
"word stuck" = STUCK
想法是得到“#34;卡住&#34;从日志文件中进行卡住线程的计数
到目前为止,这是我的java代码:我不知道如何为此编写模式。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.*;
import org.apache.log4j.Logger;
public class StuckThread {
static Logger logger = Logger.getLogger(StuckThread.class);
public static final int RETURN_CODE_SUCCESS = 0;
public static final int RETURN_CODE_ERROR = 1;
public static void main(String[] args){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // declare as DateFormat
Calendar today = Calendar.getInstance();
Date d1 = today.getTime();
String today1 =sdf.format(d1);
System.out.println(today1);
Calendar yesterday = Calendar.getInstance();
yesterday.add(Calendar.DATE, -1);
Date d = yesterday.getTime(); // get a Date object
String yesDate = sdf.format(d); // toString for Calendars is mostly not really useful
System.out.println(yesDate);
if(yesDate.equals("2016-04-20")){
BufferedReader br = null;
try {
String sCurrentLine;
int count = 0;
//String[] arLine = new String[0];
br = new BufferedReader(new FileReader("C:\\testing.txt"));
String line = "enterprises.140.625.100.50=[STUCK] ExecuteThread: '0' for queue: 'weblogic.kernel.Default<self-tuning>;
String pattern = " ";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
String stuckGroup=null;
if (m.find()) {
stuckGroup=m.group(5);
}
while ((sCurrentLine = br.readLine()) != null) {
if (sCurrentLine.contains(stuckGroup)){
count++;
}
}
logger.info("total count of stuck thread: "+count);
System.exit(RETURN_CODE_SUCCESS);
} catch (IOException e) {
e.printStackTrace();
System.exit(RETURN_CODE_ERROR);
}finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}
坚持写作模式不确定如何写。我可以帮助我吗?
答案 0 :(得分:0)
以下模式:
String pattern = "(\\w+)\\.(.*)=\\[(.*)\\]";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
if (m.find()) {
System.out.println(m.group(1));
System.out.println(m.group(2));
System.out.println(m.group(3));
}
返回:
enterprises
140.625.100.50
STUCK
这有帮助吗?