简体   繁体   English

从Java中的特定时间戳读取日志文件

[英]Read log file from a particular timestamp in java

I am reading a log file line by line (using BufferedReader) & extracting data from each line. 我正在逐行读取日志文件(使用BufferedReader)并从每一行提取数据。 The log file has some ip, timestamp on each line & the service that was invoked at that time & the status of the call. 日志文件在每行上都有一些ip,时间戳,当时所调用的服务以及呼叫状态。

Sample line from the log: 日志中的示例行:

10.435.234.220 - - [14/Jul/2014:08:07:07 -0700] "GET /resources/getQuote HTTP/1.1" 200 962 10.435.235.123 - - [14/Jul/2014:09:31:41 -0700] "GET /resources/getOpp HTTP/1.1" 200 962 10.435.234.220--[14 / Jul / 2014:08:07:07 -0700]“ GET / resources / getQuote HTTP / 1.1” 200962 10.435.235.123--[14 / Jul / 2014:09:31:41- [0700]“ GET / resources / getOpp HTTP / 1.1” 200962

The above is working fine. 以上工作正常。

But the next time I run the code, i want to extract data only from lines after the timestamp that was read the last time. 但是下一次我运行代码时,我只想从上次读取的时间戳之后的行中提取数据。

Say, The first time, it extracted data till the line with timestamp 14/Jul/2014:08:07:07, the next time, i want to extract data after that line. 说,第一次,它提取数据直到时间戳记为14 / Jul / 2014:08:07:07的行,下一次,我想在该行之后提取数据。 How to do this. 这个怎么做。 Any help will be great. 任何帮助都会很棒。

(sorry cannot comment yet) (对不起,还不能发表评论)

You need to persist the state between the executions. 您需要在两次执行之间保留状态。
Just save the last line, or the line number to a file, or database. 只需将最后一行或行号保存到文件或数据库中即可。

The very next run, start reading from the line# you saved. 接下来的运行,从保存的行号开始读取。

Here is an example of reading file from a specific line#: java: how to use bufferedreader to read specific line 这是从特定行读取文件的示例#: Java:如何使用bufferedReader读取特定行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM