【问题标题】:Rearrange a log file based on keywords根据关键字重新排列日志文件
【发布时间】:2016-05-06 14:12:50
【问题描述】:

问题:

我经常处理大型日志文件,我想将一些偶尔写入文件的相关数据整理在一起,以便更容易地跟踪问题。

写入日志文件的数据示例:

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.

我很自然地选择使用 (default task-NUMBER) 作为我的标识符,用于将相关行分组在一起。

我已经构建了我的 UI,我的想法是我会从源日志文件中提取一个片段,然后将其粘贴到我的 Java 应用程序中的 jTextArea 中,单击一个按钮并像魔术一样所有相关的任务编号(默认任务-NUMBER) 将被组合在一起。 (起初,一个简单的println 分组数据到我的 IDE 中当然是完美的)

我目前正在研究将所有文本粘贴到 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.

【问题讨论】:

  • 所以你想获取默认任务编号列表?
  • 我需要整行,我会将其添加到我的原始帖子中,谢谢!
  • 对不起,我不太清楚...您需要根据默认任务编号获取每一整行吗?
  • 嗨 Cukic0d,我已经更新了我的原始帖子,底部显示了预期的结果,我认为现在应该有意义了。谢谢

标签: java arrays string sorting jtextarea


【解决方案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);
    }
}

【讨论】:

  • 非常感谢您在这方面的帮助,我会尽快回复这个问题,如果我做对了再报告。
  • 由于某种原因我的 IDE 不满意:HashMap&lt;Integer, List&lt;String&gt;&gt; map = new HashMap&lt;Integer, List&lt;String&gt;&gt;();
  • EDIT 抱歉修复了我导入错误:) java.awt.List 而不是 java.util.List
【解决方案2】:

也许您可以检查default task-x 并获取x,将其解析为int 并将该行添加到myStringArray[x-1] = myStringArray[x-1] + "\n" + theLine; 然后从字符串中删除第一行。

【讨论】:

    【解决方案3】:

    感谢 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");
                });
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 2020-11-23
      • 2016-10-06
      • 1970-01-01
      • 2020-11-12
      • 1970-01-01
      • 2018-09-08
      相关资源
      最近更新 更多