diff options
author | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2012-01-04 12:02:26 (GMT) |
---|---|---|
committer | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2012-01-04 12:02:26 (GMT) |
commit | 23b94d0b989d374cefaa6b81ed89301c65a0916a (patch) | |
tree | 897d41e9b324a8f55ab98353492161d7c8ab62f1 /Lib/test/test_logging.py | |
parent | 239a0429fdd8af2ff8a8a5fccbc19fe640d992b5 (diff) | |
download | cpython-23b94d0b989d374cefaa6b81ed89301c65a0916a.zip cpython-23b94d0b989d374cefaa6b81ed89301c65a0916a.tar.gz cpython-23b94d0b989d374cefaa6b81ed89301c65a0916a.tar.bz2 |
Refactored logging rotating handlers for improved flexibility.
Diffstat (limited to 'Lib/test/test_logging.py')
-rw-r--r-- | Lib/test/test_logging.py | 53 |
1 files changed, 50 insertions, 3 deletions
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 5e192d5..7aa68ef 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -46,6 +46,7 @@ import time import unittest import warnings import weakref +import zlib try: import threading # The following imports are needed only for tests which @@ -3587,15 +3588,61 @@ class RotatingFileHandlerTest(BaseFileTest): rh.close() def test_rollover_filenames(self): + def namer(name): + return name + ".test" rh = logging.handlers.RotatingFileHandler( self.fn, backupCount=2, maxBytes=1) + rh.namer = namer rh.emit(self.next_rec()) self.assertLogFile(self.fn) rh.emit(self.next_rec()) - self.assertLogFile(self.fn + ".1") + self.assertLogFile(namer(self.fn + ".1")) rh.emit(self.next_rec()) - self.assertLogFile(self.fn + ".2") - self.assertFalse(os.path.exists(self.fn + ".3")) + self.assertLogFile(namer(self.fn + ".2")) + self.assertFalse(os.path.exists(namer(self.fn + ".3"))) + rh.close() + + def test_rotator(self): + def namer(name): + return name + ".gz" + + def rotator(source, dest): + with open(source, "rb") as sf: + data = sf.read() + compressed = zlib.compress(data, 9) + with open(dest, "wb") as df: + df.write(compressed) + os.remove(source) + + rh = logging.handlers.RotatingFileHandler( + self.fn, backupCount=2, maxBytes=1) + rh.rotator = rotator + rh.namer = namer + m1 = self.next_rec() + rh.emit(m1) + self.assertLogFile(self.fn) + m2 = self.next_rec() + rh.emit(m2) + fn = namer(self.fn + ".1") + self.assertLogFile(fn) + with open(fn, "rb") as f: + compressed = f.read() + data = zlib.decompress(compressed) + self.assertEqual(data.decode("ascii"), m1.msg + "\n") + rh.emit(self.next_rec()) + fn = namer(self.fn + ".2") + self.assertLogFile(fn) + with open(fn, "rb") as f: + compressed = f.read() + data = zlib.decompress(compressed) + self.assertEqual(data.decode("ascii"), m1.msg + "\n") + rh.emit(self.next_rec()) + fn = namer(self.fn + ".2") + with open(fn, "rb") as f: + compressed = f.read() + data = zlib.decompress(compressed) + self.assertEqual(data.decode("ascii"), m2.msg + "\n") + self.assertFalse(os.path.exists(namer(self.fn + ".3"))) rh.close() class TimedRotatingFileHandlerTest(BaseFileTest): |