diff --git a/log_watcher.py b/log_watcher.py index d5c9ae7..df865ad 100644 --- a/log_watcher.py +++ b/log_watcher.py @@ -7,6 +7,7 @@ from typing import Callable, Awaitable, Optional class LogWatcher: POLL_INTERVAL = 0.1 FILE_RETRY_INTERVAL = 1.0 + SEED_BYTES = 8192 # read last ~8 KB on open to seed initial state def __init__( self, @@ -35,7 +36,11 @@ class LogWatcher: while True: try: with open(self._path, 'r', errors='replace') as f: - f.seek(0, 2) # start at end of file (tail -f behaviour) + f.seek(0, 2) + end = f.tell() + f.seek(max(0, end - self.SEED_BYTES)) + if f.tell() > 0: + f.readline() # discard partial first line offline_since = None while True: line = f.readline()