[英]parse the log4j pattern in java
我有日志文件生成的以下轉換模式。
%d [%t]% - 7p%10c {1} - %m%n
我想要的是在我的程序中采用這種模式並根據此解析日志行,可能是使用正則表達式或任何東西。
這是該消息格式的正則表達式解析器:
public static void main(String[] args) {
// %d [%t] %-7p %10c{1} - %m%n
// ISO8601_date
// space
// [
// thread_id
// ]
// space
// priority
// space
// category (basename)
// space
// -
// space
// message
// line.separator
String[] samples = {
"1999-11-27 15:49:37,459 [thread-x] ERROR mypackage - Catastrophic system failure"
};
String regex = "(\\d{4}-\\d{2}-\\d{2}) (\\d{2}:\\d{2}:\\d{2},\\d{3}) \\[(.*)\\] ([^ ]*) ([^ ]*) - (.*)$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(samples[0]);
if (m.matches() && m.groupCount() == 6) {
String date = m.group(1);
String time = m.group(2);
String threadId = m.group(3);
String priority = m.group(4);
String category = m.group(5);
String message = m.group(6);
System.out.println("date: " + date);
System.out.println("time: " + time);
System.out.println("threadId: " + threadId);
System.out.println("priority: " + priority);
System.out.println("category: " + category);
System.out.println("message: " + message);
}
}
輸出是:
date: 1999-11-27
time: 15:49:37,459
threadId: thread-x
priority: ERROR
category: mypackage
message: Catastrophic system failure
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.