java 中无法解析日志文件常规异常失败

Could not parse the log file regular exception fails in java

我正在尝试解析日志文件以查找异常,然后将其存储到队列中。稍后我将该队列内容刷新到另一个文件以备将来使用。当我使用 m.find() 时,日志中的值返回为 true,但 else 语句仍在执行。在我的输入文件中,我有正常的 android 异常转储跟踪。

BufferedReader br = null;
try {
    br = new BufferedReader(new FileReader("input.txt"));
} catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
String expression = "(\d\d\d\d)";
Queue<String> q= new LinkedList<String>();
try{
    StringBuilder sb= new StringBuilder();
    String line = br.readLine();
    Pattern r = Pattern.compile(expression);
    while(line!= null)
    {
        Matcher m = r.matcher(line);
        System.out.println("Reading data"+ m.find());
        if(m.find()){
            System.out.println("SB length " + sb.length() +"sb.tostring"+sb.toString() );

            if(sb.length()>0)
            {
                String trace= sb.toString();
                q.add(trace);
                sb.setLength(0);
            }

            //   sb.append(System.lineSeparator());
        }else{
            System.out.println("Appending line " + line);
            sb.append(line);
            System.out.println("SB length " + sb.length());
            System.out.println(" line \n " + line);
        }
        line = br.readLine();

    }
    br.close();

请大家帮忙指出哪里做错了

输入文件包含以下日志:

2014-05-16 11:53:21,403 [main] INFO  org.quartz.impl.jdbcjobstore.JobStoreTX - Using db table-based data access locking (synchronization).
2014-05-16 11:53:21,407 [main] INFO  org.quartz.impl.jdbcjobstore.JobStoreTX - JobStoreTX initialized.
2014-05-16 11:53:21,407 [main] INFO  org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'Aggrergator' initialized from default resource file in Quartz package: 'quartz.properties'
2014-05-16 11:53:21,407 [main] INFO  org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 1.6.5
2014-05-16 11:53:21,612 [main] WARN  net.bull.javamelody - exception while collecting data
java.io.IOException: JavaMelody directory can't be created: /var/log/AdGainMgmt_L201
    at net.bull.javamelody.JRobin.init(JRobin.java:145)
    at net.bull.javamelody.JRobin.<init>(JRobin.java:87)
    at net.bull.javamelody.JRobin.createInstance(JRobin.java:135)
    at net.bull.javamelody.Collector.getCounterJRobin(Collector.java:759)
    at net.bull.javamelody.Collector.collectJRobinValues(Collector.java:425)
    at net.bull.javamelody.Collector.collectJavaInformations(Collector.java:336)
    at net.bull.javamelody.Collector.collect(Collector.java:289)
    at net.bull.javamelody.Collector.collectWithoutErrors(Collector.java:275)

When I am using m.find() value is returned to be true in the logs but still the else statement is getting executed.

看看你的那部分代码

System.out.println("Reading data"+ m.find());
if(m.find())
   ...
else
   ...

现在阅读该方法的文档

Attempts to find the next subsequence of the input sequence that matches the pattern. This method starts at the beginning of this matcher's region, or, if a previous invocation of the method was successful and the matcher has not since been reset, at the first character not matched by the previous match.

如果你还不明白你的错误,请查看find方法的源代码

public boolean find() {
    int nextSearchIndex = last;
    if (nextSearchIndex == first)
        nextSearchIndex++;

    // If next search starts before region, start it at region
    if (nextSearchIndex < from)
        nextSearchIndex = from;

    // If next search starts beyond region then it fails
    if (nextSearchIndex > to) {
        for (int i = 0; i < groups.length; i++)
            groups[i] = -1;
        return false;
    }
    return search(nextSearchIndex);
}

如果你还是不明白你的错误,运行这个小测试用例

public static void main(String args[])
{
    String test = "test";
    String regex = "test";
    Pattern pattern = Pattern.compile(regex);

    Matcher matcher = pattern.matcher(test);
    if(matcher.find())
        System.out.println("found");

    if(matcher.find())
        System.out.println("found2");

}

Please help pointing out where I am doing it wrongly.

每次调用find方法时,都会跳到下一组。这就是为什么您的输出显示已找到,但始终输入 else 大小写。