Im reading a log file in Java on a Linux box on a continual schedule of 2 minutes looking for certain messages. I store the last offset (RandomAccessFile getFilePointer) and read from it onwards when I detect LastModified has changed; is this best practice or even right?
1
Assuming the following:
- this log file is only appended to
- you’re prepared to meet the possibility that the file no longer exists / shorter than your current offset
- program writing to log wouldn’t attempt to delete the log file (since you may be reading it at the time) or that it handles such situations well
You shouldn’t have much to worry about. Just remember to close the file after reading it and to open it with “r” mode only.
If it works, then it’s good enough. The only problems I see with this solution is two-fold:
- What happens if someone edits the log file? The java program will ignore these changes.
- What happens if the logger splits up the log file? The last offset file pointer will be invalid.
The first point is a bit of a stretch since it is a bad idea to edit a log file (as users are only supposed to append to the file). Second point is a common practice, that the log file is split in dates or at some amount of lines.
1