followtail
Follow a growing text file like tail -F, surviving log rotation, yielding new complete
lines. Typed, zero dependencies, sync and async, with a command line tool.
Install
pip install followtail
Usage
from followtail import follow
# Blocks forever, printing each new line as the log grows.
for line in follow("/var/log/app.log"):
print(line)
follow yields a line only once its newline has arrived, so a half written record is
held until it is complete. It keeps one descriptor open and checks the name on every
poll, so it survives every rotation style logrotate performs:
- create (rename, then a new file under the name): the old file is read to its end first, then the new one from its first byte. Nothing appended just before the rotation is lost.
- copytruncate (the file is copied, then truncated in place): the shrink is detected and reading resumes from the start.
- deletion and recreation: the follower waits for the name to come back, then reads the new file from its first byte.
# The last 10 lines, then keep following.
for line in follow("app.log", lines=10):
handle(line)
# Stop after a minute, or when another thread sets the event.
import threading
stop = threading.Event()
for line in follow("app.log", timeout=60, stop=stop):
handle(line)
Events and several files
from followtail import Line, Rotated, Truncated, follow_events
for event in follow_events(["api.log", "worker.log"], lines=0):
match event:
case Line(path, text):
print(f"{path}: {text}")
case Rotated(path) | Truncated(path):
print(f"{path} was rotated")
Events are Line, Truncated, Rotated, Missing and Appeared; each carries the
path it belongs to. Missing.reason says why a path cannot be read (No such file or directory, Permission denied, Is a directory), and Line.complete is False for
a fragment: a line cut short by a rotation, a max_line cut or partial="flush".
Async
from followtail import afollow
async for line in afollow("app.log", lines=10):
await publish(line)
To release the file promptly when leaving the loop early, wrap it in
contextlib.aclosing(afollow(...)).
Your own loop
Follower does one non-blocking step per poll(), for GUI timers, selectors or tests:
from followtail import Follower
with Follower("app.log") as follower:
for event in follower.poll():
...
Options
All keyword only, shared by follow, follow_events, afollow, afollow_events:
| Option | Default | Meaning |
|---|---|---|
poll |
0.5 |
seconds to wait after a poll that found nothing |
by |
"name" |
"name" follows the path (tail -F), "descriptor" the file first opened (tail -f) |
retry |
True |
keep waiting for a missing file; False drops it |
from_start |
False |
read existing content first |
lines |
None |
start this many lines before the end (tail -n) |
partial |
"wait" |
"flush" also emits a fragment left unterminated for a whole poll |
encoding, errors |
"utf-8", "strict" |
decoding; use errors="replace" for untrusted logs |
max_line |
None |
cut longer lines into fragments of at most this many bytes (at least 4) |
max_idle, stop, timeout |
None |
ways to end the iteration |
sleep, clock |
stdlib | injectable for tests |
Lines come without their newline; one trailing carriage return (CRLF files) is removed.
follow yields fragments as plain text too; follow_events tells them apart.
read_last_lines(path, n) returns the last lines of a file without following it.
Command line
followtail -n 5 app.log print the last 5 lines and exit
followtail -F /var/log/app.log follow by name, across rotation and deletion
followtail -n 50 -F app.log the last 50 lines, then follow
followtail -n +1 -f build.log the whole file, then follow the open file
followtail --follow=name --retry x same as -F (the value is attached with =)
followtail -F a.log b.log several files, with ==> name <== headers
followtail -F app.log --timeout 60 stop following after a minute
The options, the output and the notices on stderr follow GNU tail (file truncated, has been replaced; following new file, has become inaccessible, has appeared, directories reported and skipped). A line still being written is shown once
it has been idle for one interval, as tail shows it at once; --partial wait prints
only complete lines. Exit status is 1 on errors, 130 on Ctrl+C; a closed pipe
(| head) ends it quietly.
Windows
followtail opens files with FILE_SHARE_DELETE, so the writer can still rename, delete
or truncate a file while it is followed (a reader using the built-in open() would
block that). Two limits come from Windows itself: a writer that holds its log open with
the built-in open() cannot be renamed under it (use copytruncate, or reopen the log
after rotating), and replacing the followed name in one step (os.replace onto it)
fails while it is open; rename the old file away, then create the new one. On file
systems without POSIX delete semantics (FAT, some network shares) a deleted file keeps
its name until followtail lets go of it at its next poll, so a writer recreating it in
that instant may fail once.
Why
Tailing a log in Python usually means shelling out to tail -f or reaching for
tailer, which has had no release since 2015,
and ships no type hints. followtail behaves like GNU tail -F (its tests run both on
the same files and compare the output byte for byte and the notices word for word),
loses nothing across a create rotation, works the same on Windows, and is
mypy --strict clean.
More recipes and the exact semantics: GUIDE.md.
License
MIT. See LICENSE.
Links
- Website: https://lianes8studio.com
- GitHub: https://github.com/JaviLianes8
- LinkedIn: https://www.linkedin.com/in/jlianes/
Metadata
Release files for followtail 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| followtail-0.2.0.tar.gz | 45.0 kB | Without provenance | Details for followtail-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| followtail-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for followtail-0.2.0-py3-none-any.whl |
Total release size: 72.6 kB
Release files / followtail-0.2.0.tar.gz
| Download URL | followtail-0.2.0.tar.gz |
|---|---|
| Size | 45.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
428c1c5805a56b72e301a83cfb87d2d930fd951428c8aefbb35c9772e187e337
|
|
BLAKE2b-256 checksum How to use checksums |
ff016ee89c049d0c38756c3bf32662aa3307d28a2332e043095644480058b5be
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Release files / followtail-0.2.0-py3-none-any.whl
| Download URL | followtail-0.2.0-py3-none-any.whl |
|---|---|
| Size | 27.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e017f4fdd04c39a947633aed421524d8f0d896bfa378bd1fead68ad2f2e295e3
|
|
BLAKE2b-256 checksum How to use checksums |
6e888cc46535a147d457f0bfa4f54f4d2faa66628df42c13b5f69f95faf836b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Security for followtail 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.