如何在日志文件中的某些日期之间打印字符串 [英] How to print out a string between certain dates from log file

查看:117
本文介绍了如何在日志文件中的某些日期之间打印字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个日志文件,我需要搜索某些字符串,打印出某些日期范围之间的行。

So I've got a log file that i need to search for certain strings and print out the lines that fall in between certain date ranges.

Ive得到第一部分做了,但是卡在第二部分。我将如何在一定的日期内打印出一个字符串。所以说我想在2012-09-01和2012-09-15之间打印所有字符串

Ive got the first part done but im stuck on the second. How would i print out a string within a certain date. so say i want to print all strings between 2012-09-01 and 2012-09-15

日志示例:


 2012-09-13 16:04:22 DEBUG SID:34523 BID:1329 RID:65d33 'Starting new session'
 2012-09-13 16:04:30 DEBUG SID:34523 BID:1329 RID:54f22 'Authenticating User'
 2012-09-13 16:05:30 DEBUG SID:42111 BID:319 RID:65a23 'Starting new session'
 2012-09-13 16:04:50 ERROR SID:34523 BID:1329 RID:54ff3 'Missing Authentication token'
 2012-09-13 16:05:31 DEBUG SID:42111 BID:319 RID:86472 'Authenticating User'
 2012-09-13 16:05:31 DEBUG SID:42111 BID:319 RID:7a323 'Deleting asset with ID 543234'
 2012-09-13 16:05:32 WARN SID:42111 BID:319 RID:7a323 'Invalid asset ID'

这是我的代码到目前为止:

This is my code so far:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;


public class ReadLogs {

   public static void main(String args[]) throws FileNotFoundException{
       String line, logString ="";
       Date startDate, endDate;

       ArrayList<String> logList = new ArrayList<>();

       Scanner logScanner = new Scanner(new        File("C:\\Users\\cmccarth\\Desktop\\logs.txt"));
       while (logScanner.hasNextLine()) {
            line = logScanner.nextLine();
            logList.add(line);                  
        }

       for (String z : logList) {
//          System.out.println(z);
        }


      // This function prints out all lines containing a specific string

       for( int i = 0; i <= logList.size() - 1; i++)
       {
           logString = logList.get(i);
           if(logString.contains("16:04:22")){
               System.out.println("String Contains" +logString);
           }

       }
   }     
}


推荐答案

执行以下步骤:

1)创建2个要搜索的日期范围的Date对象(get这些日期使用简单的日期格式):

1) Create 2 Date objects with the date range you want to search in ( get these dates using Simple date format) :

 Date lowerRange; 
 Date upperRange;

2)现在循环遍历过滤的日志列表,并从您的日志文件中获取的字符串,然后得到日期作为令牌[0]。

2) Now loop through your filtered log list and okenize the string that you get from the log file and then get the date as tokens[0].

String[] tokens = logString.split(" ");
String dateStr = tokens[0];

// Convert this date to String using Simple Date Format
Date date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(dateStr);

3)现在,您可以将此日期与您必须查看的2个日期进行比较范围。

3) Now you can compare this date with the 2 dates that you have to see if it falls in the range.

if(date > lowerRange && date < upperRange)
{
  // this is the date you wanted...process the log file entry
}

请参阅以下SO发布字符串到日期转换:字符串到期

Refer the following SO post for String to Date conversion : String-to-date

这篇关于如何在日志文件中的某些日期之间打印字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆