Skip to content

Commit

Permalink
Make support for copytruncate log rotation the default.
Browse files Browse the repository at this point in the history
We print a warning if copytruncate is disabled and the log file shrinks.
Also added a --no-copytruncate flag for kicks.
  • Loading branch information
bgreenlee committed May 10, 2014
1 parent 30928ec commit 5e4fcc6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ From the command line:
-o OFFSET_FILE, --offset-file=OFFSET_FILE
File to which offset data is written (default:
<logfile>.offset).
-p, --paranoid Update the offset file every time we read a line
-p, --paranoid Update the offset file every time we read a line
(as opposed to only when we reach the end of the
file).
--no-copytruncate Don't support copytruncate-style log rotation.
Instead, if the log file shrinks, print a warning.

In your code:

Expand Down
16 changes: 13 additions & 3 deletions README.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pygtail
=======

A python "port" of `logcheck's logtail2 <http://logcheck.org>`_.
A python "port" of `logcheck's logtail2 <http://logcheck.org>`__.

Pygtail reads log file lines that have not been read. It will even
handle log files that have been rotated.
Expand All @@ -22,13 +22,15 @@ From the command line:
-o OFFSET_FILE, --offset-file=OFFSET_FILE
File to which offset data is written (default:
<logfile>.offset).
-p, --paranoid Update the offset file every time we read a line
-p, --paranoid Update the offset file every time we read a line
(as opposed to only when we reach the end of the
file).
--no-copytruncate Don't support copytruncate-style log rotation.
Instead, if the log file shrinks, print a warning.

In your code:

::
.. code:: python

from pygtail import Pygtail

Expand All @@ -42,3 +44,11 @@ Pygtail does not handle rotated logs that have been compressed. You
should configure your log rotation script so that the most recently
rotated log is left uncompressed (``logrotated``, for example, has a
``delaycompress`` option that does just that).

Build status
------------

|Build Status|

.. |Build Status| image:: https://secure.travis-ci.org/bgreenlee/pygtail.png
:target: http://travis-ci.org/bgreenlee/pygtail
8 changes: 8 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
pygtail (0.4.0) lucid; urgency=low

* Make support for copytruncate log rotation the default.
* Add --no-copytruncate commandline switch.
* Print warning if copytruncate is disabled and the log file shrinks

-- Brad Greenlee <[email protected]> Fri, 9 May 2014 17:31:00 -0700

pygtail (0.3.0) lucid; urgency=low

* Add support for copytruncate style logrotation (contributed by https://github.com/atward)
Expand Down
32 changes: 26 additions & 6 deletions pygtail/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,20 @@
import string
from optparse import OptionParser

__version__ = '0.3.0'
__version__ = '0.4.0'


class Pygtail(object):
"""
Creates an iterable object that returns only unread lines.
Keyword arguments:
offset_file File to which offset data is written (default: <logfile>.offset).
paranoid Update the offset file every time we read a line (as opposed to
only when we reach the end of the file (default: False)
copytruncate Support copytruncate-style log rotation (default: True)
"""
def __init__(self, filename, offset_file=None, paranoid=False, copytruncate=False):
def __init__(self, filename, offset_file=None, paranoid=False, copytruncate=True):
self.filename = filename
self.paranoid = paranoid
self.copytruncate = copytruncate
Expand Down Expand Up @@ -146,9 +152,18 @@ def _determine_rotated_logfile(self):
if rotated_filename and exists(rotated_filename):
if stat(rotated_filename).st_ino == self._offset_file_inode:
return rotated_filename
elif self.copytruncate and \
stat(self.filename).st_ino == self._offset_file_inode:
return rotated_filename

# if the inode hasn't changed, then the file shrank; this is expected with copytruncate,
# otherwise print a warning
if stat(self.filename).st_ino == self._offset_file_inode:
if self.copytruncate:
return rotated_filename
else:
sys.stderr.write(
"[pygtail] [WARN] file size of %s shrank, and copytruncate support is "
"disabled (expected at least %d bytes, was %d bytes).\n" %
(self.filename, self._offset, stat(self.filename).st_size))

return None

def _check_rotated_filename_candidates(self):
Expand Down Expand Up @@ -192,6 +207,9 @@ def main():
cmdline.add_option("--paranoid", "-p", action="store_true",
help="Update the offset file every time we read a line (as opposed to"
" only when we reach the end of the file).")
cmdline.add_option("--no-copytruncate", action="store_true",
help="Don't support copytruncate-style log rotation. Instead, if the log file"
" shrinks, print a warning.")

options, args = cmdline.parse_args()

Expand All @@ -200,7 +218,9 @@ def main():

pygtail = Pygtail(args[0],
offset_file=options.offset_file,
paranoid=options.paranoid)
paranoid=options.paranoid,
copytruncate=not options.no_copytruncate)

for line in pygtail:
sys.stdout.write(line)

Expand Down
6 changes: 4 additions & 2 deletions pygtail/test/test_pygtail.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
import unittest
import shutil
import tempfile
Expand Down Expand Up @@ -71,8 +72,9 @@ def test_copytruncate_off_smaller(self):
self.copytruncate()
new_lines = "4\n5\n"
self.append(new_lines)
pygtail = Pygtail(self.logfile.name)
pygtail = Pygtail(self.logfile.name, copytruncate=False)
self.assertEqual(pygtail.read(), None)
self.assertRegexpMatches(sys.stderr.getvalue(), r".*?\bWARN\b.*?\bshrank\b.*")

def test_copytruncate_on_smaller(self):
self.test_readlines()
Expand All @@ -99,7 +101,7 @@ def test_copytruncate_larger_on(self):


def main():
unittest.main()
unittest.main(buffer=True)


if __name__ == "__main__":
Expand Down

0 comments on commit 5e4fcc6

Please sign in to comment.