Note that there are some explanatory texts on larger screens.

plurals
  1. PORandomAccessFile issue
    primarykey
    data
    text
    <p>I have to listern a file, when its content is added, I will read the new line, and work on the content of the new line. The file's length will never decrease.(in fact, it is the tomcat log file).</p> <p>I use the following codes:</p> <hr> <pre><code>import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import org.apache.log4j.Logger; import com.zjswkj.analyser.ddao.LogEntryDao; import com.zjswkj.analyser.model.LogEntry; import com.zjswkj.analyser.parser.LogParser; public class ListenTest { private RandomAccessFile raf; private long lastPosition; private String logEntryPattern = "^([\\d.]+) (\\S+) (\\S+) \\[([\\w:/]+\\s[+\\-]\\d{4})\\] \"(.+?)\" (\\d{3}) (\\S+) \"([^\"]+)\" \"([^\"]+)\""; private static Logger log = Logger.getLogger(ListenTest.class); public void startListenLogOfCurrentDay() { try { if (raf == null) raf = new RandomAccessFile( "/tmp/logs/localhost_access_log.2010-12-20.txt", "r"); String line; while (true) { raf.seek(lastPosition); while ((line = raf.readLine()) != null) { if (!line.matches(logEntryPattern)) { // not a complete line,roll back lastPosition = raf.getFilePointer() - line.getBytes().length; log.debug("roll back:" + line.getBytes().length + " bytes"); if (line.equals("")) continue; log.warn("broken line:[" + line + "]"); Thread.sleep(2000); } else { // save it LogEntry le = LogParser.parseLog(line); LogEntryDao.saveLogEntry(le); lastPosition = raf.getFilePointer(); } } } } catch (FileNotFoundException e) { log.error("can not find log file of today"); } catch (IOException e) { log.error("IO Exception:" + e.getMessage()); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) { new ListenTest().startListenLogOfCurrentDay(); } } </code></pre> <hr> <p>Now, my problem is that, if a line which is being written to the file's new line is not completed, a dead loop will occur.</p> <p>For example, if the tomcat try to write to the file a new line:</p> <pre><code>10.33.2.45 - - [08/Dec/2010:08:44:43 +0800] "GET /poi.txt HTTP/1.1" 200 672 "-" "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8" </code></pre> <p>And when only one part of the line is written(for example:&lt;<strong>10.33.2.45 - - [08/Dec/2010:08:44:43 +0800] "GET /poi.txt HTTP/1.1" 200 672</strong>>), now since it can not match the pattern I defined,that's to say, tomcat do not complete its writing work, so I will try to roll back the filepointer, and sleep 2 seconds and then read again.</p> <p>During the sleep time,the last part of the line maybe written yet (in fact I write them rather than tomcat for test), in my opinion, randomaccessfile will read a new line which can match the pattern, however it seems not.</p> <p>Any one can have a check the codes?</p> <p><strong>NOTE</strong>: the log file's format is "combined" like this:</p> <pre><code>10.33.2.45 - - [08/Dec/2010:08:44:43 +0800] "GET /poi.txt HTTP/1.1" 200 672 "-" "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload