擅长:python、mysql、java
<p><strong>如果日志文件很容易放入内存中(这是一个合理的轮换策略),则可以很容易地执行以下操作:</p>
<pre><code>log_lines = open('logfile','r').readlines()
last_line = get_last_lineprocessed() #From some persistent storage
last_line = parse_log(log_lines[last_line:])
store_last_lineprocessed(last_line)
</code></pre>
<p>如果你做不到这一点,你可以使用类似的方法(请参阅公认答案中的seek and tell,以防你需要使用它们)<a href="https://stackoverflow.com/questions/136168/get-last-n-lines-of-a-file-with-python-similar-to-tail">Get last n lines of a file with Python, similar to tail</a></p>