Pygtail

0.14.0 · active · verified Thu Apr 16

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

import sys
import os
from pygtail import Pygtail

# Create a dummy log file for demonstration
log_file_path = 'example.log'
with open(log_file_path, 'w') as f:
    f.write('Line 1\n')
    f.write('Line 2\n')

print(f"Reading new lines from {log_file_path}:")
for line in Pygtail(log_file_path):
    sys.stdout.write(line)

# Simulate new log entries
with open(log_file_path, 'a') as f:
    f.write('Line 3 (new)\n')
    f.write('Line 4 (new)\n')

print("\nReading newly added lines:")
for line in Pygtail(log_file_path):
    sys.stdout.write(line)

# Clean up dummy log and offset files
if os.path.exists(log_file_path):
    os.remove(log_file_path)
if os.path.exists(log_file_path + '.offset'):
    os.remove(log_file_path + '.offset')

view raw JSON →