Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Retriving data from a log file  RSS feed

 
Babu Sir
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everbody,

We have a requirement that i should retrive latest data(information) from the log file.This log file will be updating ever 15 minutes.So when the user requested at particular time(say at 10.00AM),we should be able to get that data only.

Please suggest how to go forward
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Babu.

A naive strategy would be to open the log file using standard I/O and start looking for the requested timestamp (I suppose the log file has such information).
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On one small server I have a log writer that writes to a circular buffer in memory. Once it is full, the new entries overwrite the oldest. I don't know if you could afford the memory to build such a thing using age to grow and shrink the buffer - maybe a LinkedList - as time goes by. Here's a snippet from my workstation:
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another idea, in pseudocode:

Note that when you use the skip() method to skip bytes, you generally need to use a loop to make sure that it really skips all the bytes you requested:


Depending on the operating system and how your logging works, you may or may not sometimes see a file that appears to end with an incomplete line, because the file is being written at the same time you're trying to read it. If you encounter this phenomenon, perhaps you can save the last line read in memory, and add it to whatever is read the next time you read the file. Or perhaps this in an unnecessary complication. You'll have to test and see what happens, I think.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!