用 Java 解析日志文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4489330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:36:08  来源:igfitidea点击:

Parsing a log file in Java

javaparsingloggingtokenize

提问by user454894

All,

全部,

I have a log file with the below content.

我有一个包含以下内容的日志文件。

Request from Centercord.
2010-12-14 12:42:13.724 [ 6796] ****************************
2010-12-14 12:42:13.724 [ 6796] 1111111111111111
2010-12-14 12:42:13.724 [ 6796]22222222222

Response from Centercord.
2010-12-14 12:42:21.802 [ 5960] 11111111111111
2010-12-14 12:42:21.802 [ 5960]  ffffffffffffffffffffffffffff
2010-12-14 12:42:21.802 [ 5960]  tttttttttttttttttttttttttttt

Request from Centercord.
2010-12-14 12:42:13.724 [ 6796] ****************************

I need to create two log files one is for storing all the Request details and the other one is for storing all the Response details. How can I parse this and prepare two log files?.

我需要创建两个日志文件,一个用于存储所有请求详细信息,另一个用于存储所有响应详细信息。我如何解析它并准备两个日志文件?。

I need the below answer.

我需要以下答案。

Log 1:

Request from Centercord.
2010-12-14 12:42:13.724 [ 6796] ****************************
2010-12-14 12:42:13.724 [ 6796] 1111111111111111
2010-12-14 12:42:13.724 [ 6796]22222222222

2010-12-14 12:42:13.724 [ 6796] ****************************

Log 2:

Response from Centercord.
2010-12-14 12:42:21.802 [ 5960] 11111111111111
2010-12-14 12:42:21.802 [ 5960]  ffffffffffffffffffffffffffff
2010-12-14 12:42:21.802 [ 5960]  tttttttttttttttttttttttttttt

Regards, Kanagaraj

问候, 卡纳加拉吉

回答by aioobe

Here is how I would do it:

这是我将如何做到的:

import java.io.*;
import java.util.Scanner;

public class Test {

    public static void main(String[] args) {

        try {

            PrintWriter requests = new PrintWriter("requests.txt");
            PrintWriter responses = new PrintWriter("responses.txt");
            PrintWriter currentLog = null;

            Scanner s = new Scanner(new File("log.txt"));
            while (s.hasNextLine()) {
                String line = s.nextLine();
                if (line.startsWith("Request from"))
                    currentLog = requests;
                else if (line.startsWith("Response from"))
                    currentLog = responses;
                else if (currentLog != null)
                    currentLog.println(line);
            }

            requests.close();
            responses.close();
            s.close();
        } catch (IOException ioex) {
            // handle exception...
        }
    }
}

Given log.txt

给定日志.txt

Request from Centercord.
2010-12-14 12:42:13.724 [ 6796] ****************************
2010-12-14 12:42:13.724 [ 6796] 1111111111111111
2010-12-14 12:42:13.724 [ 6796]22222222222

Response from Centercord.
2010-12-14 12:42:21.802 [ 5960] 11111111111111
2010-12-14 12:42:21.802 [ 5960]  ffffffffffffffffffffffffffff
2010-12-14 12:42:21.802 [ 5960]  tttttttttttttttttttttttttttt

Request from Centercord.
2010-12-14 12:42:13.724 [ 6796] ****************************

It produces, requests.txt

它产生,requests.txt

2010-12-14 12:42:13.724 [ 6796] ****************************
2010-12-14 12:42:13.724 [ 6796] 1111111111111111
2010-12-14 12:42:13.724 [ 6796]22222222222

2010-12-14 12:42:13.724 [ 6796] ****************************

...and responses.txt

...和响应.txt

2010-12-14 12:42:21.802 [ 5960] 11111111111111
2010-12-14 12:42:21.802 [ 5960]  ffffffffffffffffffffffffffff
2010-12-14 12:42:21.802 [ 5960]  tttttttttttttttttttttttttttt

回答by iuiz

You can use regular expressions or RandomAccessFiles to get the data in the way you want.

您可以使用正则表达式或 RandomAccessFiles 以您想要的方式获取数据。

回答by Andreas Dolk

StringBuilder requestBuilder = new StringBuilder("Request from Centercord.\n");
StringBuilder responseBuilder = new StringBuilder("Response from Centercord.\n");

boolean isResponse = false;
for (String line:getLinesFromLogFile()) {
  if (line.startsWith("Response")) {
      if(isResponse)
        responseBuilder.append("\n");
      isResponse = true;
  } else if (line.startsWith("Request")) {
      if(!isResponse)
        requestBuilder.append("\n");
      isResponse = false;
  } else {
      if (isResponse) {
         responseBuilder.append(line).append("\n");
      } else {
         requestBuilder.append(line).append("\n");
      }
  }
}

Now dump the contents of both StringBuilders to the target files.

现在将两个 StringBuilder 的内容转储到目标文件。

回答by darioo

For reducing boilerplate code, download Google Guavaand use this code:

要减少样板代码,请下载Google Guava并使用以下代码:

File inputFile = new File("input.log");
File outputLog1 = new File("log1.log");
File outputLog2 = new File("log2.log");

String match1 = "[ 6796]";
String match2 = "[ 5960]";

Files.append("Request from Centercord.\n", outputLog1, Charsets.UTF_8);
Files.append("Response from Centercord.\n", outputLog2, Charsets.UTF_8);

List<String> lines = Files.readLines(inputFile, Charsets.UTF_8);

for (String line : lines) {
    if(line.contains(match1)) Files.append(line + "\n", outputLog1, Charsets.UTF_8);
    if(line.contains(match2)) Files.append(line + "\n", outputLog2, Charsets.UTF_8);
}