问题:
我经常使用大型日志文件,我想组织一些相关数据,这些数据可以偶尔写在文件中,以便更容易地跟踪问题。
写入日志文件的数据示例:
1. 2016-05-05 15:07:54,993 DEBUG (default task-1) ==> More stuff written on this line.
2. 2016-05-05 15:07:54,993 DEBUG (default task-16) ==> More stuff written on this line.
3. 2016-05-05 15:07:54,993 DEBUG (default task-2) ==> More stuff written on this line.
4. 2016-05-05 15:07:54,993 DEBUG (default task-33) ==> More stuff written on this line.
5. 2016-05-05 15:07:54,993 DEBUG (default task-1) ==> More stuff written on this line.
6. 2016-05-05 15:07:54,993 DEBUG (default task-33) ==> More stuff written on this line.
7. 2016-05-05 15:07:54,993 DEBUG (default task-33) ==> More stuff written on this line.
8. 2016-05-05 15:07:54,993 DEBUG (default task-2) ==> More stuff written on this line.
9. 2016-05-05 15:07:54,993 DEBUG (default task-33) ==> More stuff written on this line.
10. 2016-05-05 15:07:54,993 DEBUG (default task-1) ==> More stuff written on this line.
11. 2016-05-05 15:07:54,993 DEBUG (default task-6) ==> More stuff written on this line.
12. 2016-05-05 15:07:54,993 DEBUG (default task-3) ==> More stuff written on this line.
13. 2016-05-05 15:07:54,993 DEBUG (default task-2) ==> More stuff written on this line.
14. 2016-05-05 15:07:54,993 DEBUG (default task-14) ==> More stuff written on this line.
15. 2016-05-05 15:07:54,993 DEBUG (default task-1) ==> More stuff written on this line.
我自然选择使用(默认任务 - NUMBER
)作为我的标识符,用于将相关行分组在一起。
我已经构建了我的UI,我的想法是从源日志文件中获取一个摘录,然后将其粘贴到我的Java应用程序中的jTextArea
,单击一个按钮并像魔术一样将所有相关的任务编号(默认任务 - NUMBER
)将组合在一起。 (首先,我的IDE中对这些数据的分组数据的简单println
当然是完美的)
我正在研究将所有文本粘贴到jTextArea
并通过它完成的方法,创建了一个字符串数组,我可以稍后展开它以查找任意数量的任务数字,目前它突出了所有的它找到的数字,(不确定我是否在正确的道路上):
import java.awt.Color;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
public class ArrangeLogic {
public void groupLogFile(JTextArea theLogs) {
String[] myStringArray = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35"};
for (int i = 0; i < myStringArray.length - 1; i++) {
String element = myStringArray[i];
String nextElement = myStringArray[i + 1];
String defaultTaskOdd = ("(default task-" + element + ")");
String defaultTaskEven = ("(default task-" + nextElement + ")");
System.out.println(defaultTaskOdd);
System.out.println(defaultTaskEven);
try {
Document document = theLogs.getDocument();
for (int index = 0; index + defaultTaskOdd.length() < document.getLength(); index++) {
String match = document.getText(index, defaultTaskOdd.length());
if (defaultTaskOdd.equals(match)) {
javax.swing.text.DefaultHighlighter.DefaultHighlightPainter highlightPainter
= new javax.swing.text.DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
theLogs.getHighlighter().addHighlight(index, index + defaultTaskOdd.length(),
highlightPainter);
}
}
for (int index = 0; index + defaultTaskEven.length() < document.getLength(); index++) {
String match = document.getText(index, defaultTaskEven.length());
if (defaultTaskEven.equals(match)) {
javax.swing.text.DefaultHighlighter.DefaultHighlightPainter highlightPainter
= new javax.swing.text.DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
theLogs.getHighlighter().addHighlight(index, index + defaultTaskEven.length(),
highlightPainter);
}
}
} catch (BadLocationException ex) {
}
}
}
}
我一整天都在玩弄多个想法(是的,我是一个非常新手的开发人员)关于如何循环所有线路并重新组合在一起但没有运气,所以我想我会要求一些咨询。任何帮助或建议将不胜感激。感谢。
(适用修改)
预期输出(线数不重要):
1. 2016-05-05 15:07:54,993 DEBUG (default task-1) ==> More stuff written on this line.
5. 2016-05-05 15:07:54,993 DEBUG (default task-1) ==> More stuff written on this line.
10. 2016-05-05 15:07:54,993 DEBUG (default task-1) ==> More stuff written on this line.
15. 2016-05-05 15:07:54,993 DEBUG (default task-1) ==> More stuff written on this line.
2. 2016-05-05 15:07:54,993 DEBUG (default task-16) ==> More stuff written on this line.
3. 2016-05-05 15:07:54,993 DEBUG (default task-2) ==> More stuff written on this line.
8. 2016-05-05 15:07:54,993 DEBUG (default task-2) ==> More stuff written on this line.
13. 2016-05-05 15:07:54,993 DEBUG (default task-2) ==> More stuff written on this line.
4. 2016-05-05 15:07:54,993 DEBUG (default task-33) ==> More stuff written on this line.
6. 2016-05-05 15:07:54,993 DEBUG (default task-33) ==> More stuff written on this line.
7. 2016-05-05 15:07:54,993 DEBUG (default task-33) ==> More stuff written on this line.
9. 2016-05-05 15:07:54,993 DEBUG (default task-33) ==> More stuff written on this line.
11. 2016-05-05 15:07:54,993 DEBUG (default task-6) ==> More stuff written on this line.
12. 2016-05-05 15:07:54,993 DEBUG (default task-3) ==> More stuff written on this line.
14. 2016-05-05 15:07:54,993 DEBUG (default task-14) ==> More stuff written on this line.
答案 0 :(得分:1)
嗯,你不是这样做的好方法。
而不是在循环中使用循环,这很慢,你绝对应该使用正则表达式。然后我建议你使用扫描仪逐一获取每一行......
这是一个解释我是如何解决问题的工作代码,你可以为自己添加突出显示的东西......
public static void groupLogFile(JTextArea theLogs) {
//This is used to get each line one by one
Scanner sc = new Scanner(theLogs.getText());
//We are using a HashMap to store the lines in function of the task numbers
HashMap<Integer, List<String>> map = new HashMap<Integer, List<String>>();
//We are now reading each line one by one
while (sc.hasNextLine()) {
String line = sc.nextLine();
//With this regex we get at group 2 the task number
Pattern pattern = Pattern.compile("(\\(default task-(\\d+)\\))");
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
//Task number
int task_number = Integer.parseInt(matcher.group(2));
//We get other lines with same task number (if exist)
List<String> get;
if(map.containsKey(task_number)){
get = map.get(task_number);
} else {
get = new LinkedList<String>();
}
get.add(line);
//We update the list
map.put(task_number, get);
}
}
sc.close();
//Ordering the map by task number
Comparator<Integer> comparator = new Comparator<Integer>() {
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
};
TreeMap<Integer, List<String>> ordered = new TreeMap<Integer, List<String>>(comparator);
ordered.putAll(map);
//Print results
for(Entry<Integer, List<String>> e : ordered.entrySet()){
for(String s : e.getValue())
System.out.println(s);
}
}
答案 1 :(得分:0)
也许您可以查看default task-x
并获取x
,将其解析为int
并将该行添加到myStringArray[x-1] = myStringArray[x-1] + "\n" + theLine;
然后从字符串中删除第一行。
答案 2 :(得分:0)
感谢Cukic0d答案的帮助!如果有人想知道如何将结果打印到JTextArea
而不是println
:
//Clear theLogs (what was pasted into the UI)
theLogs.setText("");
//Print results
ordered.entrySet().stream().forEach((e) -> {
e.getValue().stream().map((s) -> {
//System.out.println(s);
return s;
}).forEach((s) -> {
theLogs.append(s+"\n");
});
});