Pygtail is a Python library and command-line tool that reads log file lines that have not been previously read. It is a 'port' of logcheck's logtail2 and is capable of handling log files that have been rotated. The current stable version is 0.14.0. Releases are infrequent, often tied to bug fixes or minor enhancements.
pip install pygtailVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use Pygtail to read new lines from a log file. It first creates a dummy log file, reads its initial content, then appends new lines, and finally uses Pygtail again to read only the newly added lines. Pygtail automatically manages an offset file (by default `logfile.offset`) to keep track of already read content.
Initialize `Pygtail` with `copytruncate=True` (e.g., `Pygtail('logfile.log', copytruncate=True)`). This enables support for `copytruncate`-style rotation.Specify the correct encoding when initializing `Pygtail` using the `encoding` parameter (e.g., `Pygtail('logfile.log', encoding='utf8')`).Manage the lifecycle of the offset file carefully. If you need to re-read a file from the end if the offset file is missing, use the `read_from_end=True` parameter. For temporary reading without persistent offsets, you can specify `offset_file='/dev/null'` (on Unix-like systems) or use `save_on_end=False` and manually manage offsets with `tail.with_offsets()` and `tail.write_offset_to_file()`.
Pass the `encoding` parameter to the `Pygtail` constructor: `tail = Pygtail('your.log', encoding='utf8')`. Adjust 'utf8' to the actual encoding of your log file if different.Enable `copytruncate` support by initializing `Pygtail` with `copytruncate=True`: `tail = Pygtail('your.log', copytruncate=True)`. If log rotation involves compression, `pygtail` might also need custom log patterns using the `--log-pattern` command-line option, though this is not directly supported in the Python API for dynamic patterns.Ensure the directory where the log file resides (or where you specify the offset file) has write permissions for the user running the Pygtail script. Alternatively, specify an explicit and writable `offset_file` path: `tail = Pygtail('some.log', offset_file='/var/log/my_app/some.log.offset')`.No dependency data recorded yet.