Coder Social home page Coder Social logo

derpston / python-multitail2 Goto Github PK

View Code? Open in Web Editor NEW
12.0 4.0 8.0 29 KB

Python module to follow new lines from multiple files at once, taking account of new files, deleted files, and rotated files.

License: Do What The F*ck You Want To Public License

Python 100.00%

python-multitail2's Introduction

python-multitail2

Python module to follow new lines from multiple files at once, taking account of new files, deleted files, and rotated files.

Suitable for following sets of logs where you don't necessarily know how many files there are, what their names are, and don't want to have to keep track of changes yourself.

Doesn't (yet) support inotify for detecting file change events because of portability concerns and the lack of an inotify module in the Python standard library.

Another one?

Yeah, another one. Other multitail implementations I found didn't support automatically following new files.

Features

  • Accepts globbing syntax, like /var/log/*.log
  • Opens new files
  • Closes deleted files
  • Reopens rotated files
  • Non-blocking
  • Supports iteration

Examples

Emits ((path, offset), line) tuples representing the path to the file that each line comes from, along with a byte offset where the line begins.

Generator

>>> import multitail2
>>> mt = multitail2.MultiTail("/home/user/test/*")
>>> for line in mt:
...  print line
... 
(('/home/user/test/foo', 0), 'bar')
(('/home/user/test/foo', 4), 'bar')

Non-blocking polling

>>> import multitail2
>>> mt = multitail2.MultiTail("/home/user/test/*")
>>> list(mt.poll())
[(('/home/user/test/foo', 0), 'bar')]
>>> list(mt.poll())
[]

TODO

  • Use inotify, where available. Should not be a hard dependency.

Bugs

  • Does not handle truncated files - new content will be ignored until the file size exceeds what it was previously.

Contributing

Contributions welcome!

python-multitail2's People

Contributors

derpston avatar erimf avatar qrilka avatar superman32432432 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

python-multitail2's Issues

windows support for non-blocking tailing

Thanks for the library, it's great. Just a small one for windows users getting the following, this can be fixed by removing the non-block flag in the os.open call. Not sure if there is a better way but this appears to fix the problem for me, and works for tailing multiple files

Patch:

import sys

is_windows = sys.platform == 'win32'
fh = os.fdopen(os.open(path, os.O_RDONLY | (0 if is_windows else os.O_NONBLOCK)))

Error:

Traceback (most recent call last):
File "userfile.py", line 6, in
mt = multitail2.MultiTail(glob_str)
File "C:\Python310\lib\site-packages\multitail2.py", line 152, in init
self._rescan(skip_to_end = skip_to_end)
File "C:\Python310\lib\site-packages\multitail2.py", line 177, in _rescan
self._tailedfiles[path] = TailedFile(path, skip_to_end = skip_to_end, offset = self._offsets.get(path, None))
File "C:\Python310\lib\site-packages\multitail2.py", line 16, in init
self._open(path, skip_to_end, offset)
File "C:\Python310\lib\site-packages\multitail2.py", line 30, in _open
fh = os.fdopen(os.open(path, os.O_RDONLY | os.O_NONBLOCK))
AttributeError: module 'os' has no attribute 'O_NONBLOCK'

skipping long lines

There appears to be a bug in handling skipping long lines. I think there should be an else condition before returning in ValueError. That is, long lines are checking in "if not at_eof" and then there should be an "else: return" if we are at_eof. Currently any long line will stop all further evaluation (e.g. if using poll()).

Separately, it would be great to be able to handle long lines in general. For example, this comes up in FIX logs when receiving security definitions. Perhaps there could be an option that emulates fh.readline()?

License?

Could you add a license for this project (or declare it freeware somewhere if you don't intend on having a traditional license)?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.