Skip to main content

a unix tail implementation in python

Project description

pythontail-logo

PythonTail: a Unix tail implementation in python. Version: 0.9


Table of Contents


Getting Started

Unix version comparison

Unix tail implementation list (adapted)

  • item: default output line = 10
    descrtption: default output with 10 lines for each tailed file
    status: [OK]
  • item: -c, --bytes=[+]NUM
    description: output the last NUM bytes; or use -c +NUM to output starting
    with byte NUM of each file
    status: [PENDING]
  • item: -f, --follow
    description: output appended data as the file grows;
    status: [OK]
  • item: -n, --lines=NUM
    description: output the last NUM lines, instead of the last 10; or use -n
    status: [OK]
  • item: --max-unchanged-stats=N
    description: with --follow=name, reopen a FILE which has not
    changed size after N (default 5) iterations to see if it has
    been unlinked or renamed (this is the usual case of rotated
    log files); with inotify, this option is rarely useful
    status: [PENDING]
  • item: --pid=PID
    description: with -f, terminate after process ID, PID dies
    status: [PENDING]
  • item: -q, --quiet, --silent
    description: never output headers giving file names
    status: [OK]
  • item: --retry
    description: keep trying to open a file if it is inaccessible
    status: [PENDING]
  • item: -s, --sleep-interval=N
    description: with -f, sleep for approximately N seconds (default 0)
    between iterations; at least once every N seconds
    status: [OK]
  • item: -v, --verbose
    description: always output headers giving file names
    status: [PENDING]
  • item: -z, --zero-terminated
    description: line delimiter is NUL, not newline
    status: [PENDING]
  • item: --help
    description: display this help and exit
    status: [OK]
  • item: --version
    description: output version information and exit
    status: [OK]
  • item: multiple files support
    description: tail as many files as wanted with all available parameters working as well
    status: [OK]

Unique tail implementations

  • item: usage as both terminal command and as python module
    description: the use of all parameters within terminal command line
    and by importing as a module inside a python script
    status: [OK]
  • item: -d, --debug
    description: create a log file and register all ocurrencies of regular
    behavior information, debug and errors as the timestamp and logged user
    status: [OK]
  • item: --logless
    description: send all output to stdout with no log folder or file being created
    status: [OK]
  • item: ?
    description: tail files over tcp/ip
    status: [PENDING]

Via Bash

Installation Process

install as a module:

pip install pythontail

and use it as:

from pythontail import tail
tail.run(["dir/log/file.log"])

use as terminal command:

git clone https://github.com/natanaelfneto/pythontail.git
python pythontail.py -h

enjoy


Usage

this messagem can also be found with python pythontail.py -h command

usage: pythontail.py [-h] [-f | -n LINES] [-q] [-s SLEEP] [-d] [-v] sources [sources ...]

Unix tail implementation in python 

optional arguments:
-h, --help                  show this help message and exit
-f, --follow                flag to not limit number of lines tailed
-n LINES, --lines LINES     number of lines to follow in total array of sources

-q, --quiet, --silent       never output headers giving file names

-s SLEEP, --sleep SLEEP, --sleep-interval SLEEP
                            with --follow, sleep for approximately N seconds
                            (default 0) between iterations; least once every N seconds

-d, --debug                 process debug flag
-v, --version               output software version

Examples

First we need a fake log file

Generate many live fake logs files by running:

python pythontail/test/fake_log_generator.py <integer> &

for the example1.py, generate the minimum of 2 fake log files
This will create two fake_n.log files in ~/log/fake_n.log that can be tailed

Using it as console command for tail files

python pythontail/pythontail.py -f ~/log/fake_1.log ~/log/fake_2.log

Using it as python module for tail files

Create a virtual env and activate it (can be pyenv or virtualenv or any other)

mkvirtualenv pythontail
workon pythontail

Install pythontail module

pip install pythontail

Create your code as the available in pythontail/examples/example1.py\

from pythontail import tail
from pathlib import Path

import os


# get as many valid files paths you want to be tailed
log_file_1 = str(Path(f"{os.path.expanduser("~")}/log/fake_1.log"))
log_file_2 = str(Path(f"{os.path.expanduser("~")}/log/fake_2.log"))

# tail them
tail.run(
    # debug=False,
    # quiet=False,
    # lines=10,
    # sleep=0,
    sources=[
        log_file_1, 
        log_file_2
    ]
)

# for a --follow atribute, set the maximum number of lines to zero [lines=0]
# optional --sleep atribute only works with --follow flag, done by making [lines=0]

Check the output of example1 by running the script file:

python pythontail/examples/example1.py

License

MIT License

Copyright (c) 2017 Natanael F. Neto (natanaelfneto)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pythontail-0.9.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

pythontail-0.9-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file pythontail-0.9.tar.gz.

File metadata

  • Download URL: pythontail-0.9.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.0

File hashes

Hashes for pythontail-0.9.tar.gz
Algorithm Hash digest
SHA256 fa282a9e9bce8006a9c23ddf492917e77b9ab53beed7496e9cfbd50e8a6458a4
MD5 ce5235454f32efdd578c8214cf5dae0c
BLAKE2b-256 c20fefbc30424d8284570af829ad6189f53e9d0ddaac1a5166282eb4ec3a5d49

See more details on using hashes here.

File details

Details for the file pythontail-0.9-py3-none-any.whl.

File metadata

  • Download URL: pythontail-0.9-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.0

File hashes

Hashes for pythontail-0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 c491399becd23c7b167ed93da9835a4395f03e26600c78551064cdf6a2406e89
MD5 8989d61b8d24c6414b3e6f4be85a35eb
BLAKE2b-256 8a2f4c8585f662a3975952271722f7e5d0bf76696d7f7a13ddad46c3c71c639b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page