java regex从日志文件中提取日志

时间:2015-12-09 09:10:42

标签: java regex

我有一个6MB的日志文件,带有TRACE INFO DEBUG ERROR日志级别,我想创建一个java正则表达式来获取具有指定日志级别的所有日志。

我想获取级别DEBUG的日志,然后只提取调试类型。 或者日志详细信息可能有多行详细信息。

以下是日志文件详细信息:

* DEBUG [2015/11/25 00:06:59] /usr/local/elsa/node/elsa.pl (184) main:: 22497 [undef]
Starting process_batch
* DEBUG [2015/11/25 00:06:59] /usr/local/elsa/node/elsa.pl (271) main::_process_batch 22497 [undef]
Offline processing:  and using tempfile /data/elsa/tmp/buffers/1448438819.71967
* ERROR [2015/11/25 00:06:59] /usr/local/elsa/node/elsa.pl (295) main::_process_batch 22497 [undef]
Unable to parse log line: .  Only parsed into:
$VAR1 = [];
* DEBUG [2015/11/25 00:06:59] /usr/local/elsa/node/elsa.pl (316) main::_process_batch 22497 [undef]
Finished job process_batch with 0 logs processed and 0 new programs
* DEBUG [2015/11/25 00:06:59] /usr/local/elsa/node/elsa.pl (318) main::_process_batch 22497 [undef]
file size for file /data/elsa/tmp/buffers/1448438819.71967 is 0
* TRACE [2015/11/25 00:06:59] /usr/local/elsa/node/elsa.pl (327) main::_process_batch 22497 [undef]
No logs recorded
* DEBUG [2015/11/25 00:06:59] /usr/local/elsa/node/elsa.pl (188) main:: 22497 [undef]
Processed 0 records
* INFO [2015/11/25 00:07:00] /usr/local/elsa/node/elsa.pl (206) main:: 22497 [undef]
Exiting after processing 0 records
* TRACE [2015/11/25 00:07:04] /usr/local/elsa/node/elsa.pl (135) main:: 22520 [undef]
Processing file __OPS__...
* DEBUG [2015/11/25 00:07:04] /usr/local/elsa/node/elsa.pl (271) main::_process_batch 22520 [undef]
Offline processing:  and using tempfile /data/elsa/tmp/buffers/ops_1448438824.46437
* TRACE [2015/11/25 00:07:04] /usr/local/elsa/node/elsa.pl (135) main:: 22526 [undef]
Processing file __IMPORT__...
* INFO [2015/11/25 00:07:04] /usr/local/elsa/node/Indexer.pm (332) Indexer::initial_validate_directory 22522 [undef]
Deleting stale ops log /data/elsa/tmp/buffers/ops_1448438824.46437
* DEBUG [2015/11/25 00:07:04] /usr/local/elsa/node/Indexer.pm (364) Indexer::initial_validate_directory 22522 [undef]
files: $VAR1 = [
          '/data/elsa/tmp/buffers/.',
          '/data/elsa/tmp/buffers/..'
        ];
* DEBUG [2015/11/25 00:07:04] /usr/local/elsa/node/Indexer.pm (366) Indexer::initial_validate_directory 22522 [undef]
considering file /data/elsa/tmp/buffers/.
* DEBUG [2015/11/25 00:07:04] /usr/local/elsa/node/Indexer.pm (366) Indexer::initial_validate_directory 22522 [undef]
considering file /data/elsa/tmp/buffers/..
* TRACE [2015/11/25 00:07:04] /usr/local/elsa/node/Indexer.pm (1782) Indexer::_get_lock 22522 [undef]
Locked directory
* DEBUG [2015/11/25 00:07:04] /usr/local/elsa/node/Indexer.pm (567) Indexer::_validate_directory 22522 [undef]
Wiping via index temp_1
* TRACE [2015/11/25 00:07:04] /usr/local/elsa/node/Indexer.pm (2195) Indexer::_sphinx_index 22522 [undef]
Starting Sphinx indexing for temp_1
* DEBUG [2015/11/25 00:07:04] /usr/local/elsa/node/Indexer.pm (2201) Indexer::_sphinx_index 22522 [undef]
output: Sphinx 2.1.9-id64-release (rel21-r4761)
Copyright (c) 2001-2014, Andrew Aksyonoff
Copyright (c) 2008-2014, Sphinx Technologies Inc (http://sphinxsearch.com)

然后我想只获得指定级别的日志... 我使用以下代码,但它无法正常工作。

public class T {
    public static CharSequence fromFile(String filename) throws IOException {
        FileInputStream input = new FileInputStream(filename);
        FileChannel channel = input.getChannel();
        ByteBuffer bbuf = channel.map(FileChannel.MapMode.READ_ONLY, 0, (int)channel.size());
        CharBuffer cbuf = Charset.forName("8859_1").newDecoder().decode(bbuf);
        input.close();
        return cbuf;
    }

    public static void main(String[] args) {
        try {
            Pattern pattern = Pattern.compile("((DEBUG.*?)(([\n].*?)+)((TRACE)|(ERROR)|(INFO)))");
            Matcher matcher = pattern.matcher(fromFile("d:/log.txt"));
            int count = 0;
            while (matcher.find()) {
                String match = matcher.group();
                System.out.println("::: "+match.substring(0,match.lastIndexOf(" ")-1));
                count++;
            }
            System.out.println("Total Matches : "+count);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

我希望所有日志都有级别DEBUG 还有一些其他日志文件不能有'*'模式

请帮忙 谢谢advancd

3 个答案:

答案 0 :(得分:0)

如果您只想提取DEBUG行,请尝试:

DEBUG[^*]*

DEMO

您可以通过更改其他任何内容的DEBUG来查找日志信息类型。所以主要部分是:

TYPE[^*]*

答案 1 :(得分:0)

((\ bTRACE \ B'EM>)((\ r \ n]的)+)((DEBUG)|。?。?(ERROR)|(INFO)|($$)) )

也使用喜欢。 。 。

Pattern pattern = Pattern.compile("((\\bTRACE\\b.*?)(([\r\n].*?)+)((DEBUG)|(ERROR)|(INFO)|($$)))");

            Matcher matcher = pattern.matcher(fromFile("d:/demo.txt"));
            int count = 0;
            while (matcher.find()) {
                String match = matcher.group();
                System.out.println("::: "+match.substring(0,match.lastIndexOf(" ")-1));
                count++;
            }

答案 2 :(得分:0)

使用java代码我们就像这样完成这个任务。 (我上周末真的很挣扎)。 所以我发布解决方案。

    private String logLevelDenied1;
    private String logLevelDenied2;
    private String logLevelDenied3;
    private String logLevelDenied4;


private List<String> readLogFile(String level , LogFilesEnum logFile , int count) throws Exception {
        FileInputStream fileInputStream = null;
        BufferedReader bufferedReader = null;
        try{
            fileInputStream = new FileInputStream(getLogFileLocation(logFile));
            bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
            String tempString;
            int recordCount = 0;
            boolean flag = true;
            setDeniedVariables(level);
            if(logFile == LogFilesEnum.QUERY || logFile == LogFilesEnum.SEARCHD) {
                level = "";
            }
            List<String> logDetails = new ArrayList<String>(); 
            while (((tempString = bufferedReader.readLine()) != null) && (recordCount < count)) {
                if (tempString.contains(level)) {
                    flag = true;
                    recordCount++;
                    logDetails.add(tempString);
                    while (flag && recordCount<count) {
                        tempString = bufferedReader.readLine();
                        if (tempString.contains(logLevelDenied1) || tempString.contains(logLevelDenied2) || tempString.contains(logLevelDenied3) || tempString.contains(logLevelDenied4))
                            flag = false;
                        else {
                            if(tempString.contains(level))
                                recordCount++;
                            logDetails.add(tempString);
                        }
                    }
                }   
            }
            return logDetails;
        } catch(Exception e){
            e.printStackTrace();
            return null;
        } finally{
            if (fileInputStream != null)
                 fileInputStream.close();
            if (bufferedReader != null)
                 bufferedReader.close();
        }
    }





private void setDeniedVariables(String logLevel) {
        switch (logLevel.toUpperCase()) {
            case "ERROR":
                logLevelDenied1 = "TRACE";
                logLevelDenied2 = "DEBUG";
                logLevelDenied3 = "WARN";
                logLevelDenied4 = "INFO";
                break;
            case "TRACE":
                logLevelDenied1 = "ERROR";
                logLevelDenied2 = "DEBUG";
                logLevelDenied3 = "WARN";
                logLevelDenied4 = "INFO";   
                break;
            case "INFO":
                logLevelDenied1 = "TRACE";
                logLevelDenied2 = "DEBUG";
                logLevelDenied3 = "WARN";
                logLevelDenied4 = "ERROR";
                break;
            case "WARN":    
                logLevelDenied1 = "TRACE";
                logLevelDenied2 = "DEBUG";
                logLevelDenied3 = "ERROR";
                logLevelDenied4 = "INFO";
                break;
            case "DEBUG":   
                logLevelDenied1 = "TRACE";
                logLevelDenied2 = "ERROR";
                logLevelDenied3 = "WARN";
                logLevelDenied4 = "INFO";
                break;
            default :
                logLevelDenied1 = "--NONE--";
                logLevelDenied2 = "--NONE--";
                logLevelDenied3 = "--NONE--";
                logLevelDenied4 = "--NONE--";
        }
    }