fixed logger tests.

This commit is contained in:
fawce
2012-05-24 21:25:52 -04:00
parent fde665f3ec
commit 6bfa4bfddc
2 changed files with 34 additions and 31 deletions
+2 -31
View File
@@ -1,7 +1,7 @@
import logging import logging
import uuid import uuid
from zipline.utils.logger import configure_logging from zipline.utils.logger import configure_logging, tail
from unittest2 import TestCase from unittest2 import TestCase
@@ -17,35 +17,6 @@ class LoggerTestCase(TestCase):
self.LOG.info(test_msg) self.LOG.info(test_msg)
logfile = open('/var/log/zipline/zipline.log','r') logfile = open('/var/log/zipline/zipline.log','r')
with logfile: with logfile:
last_line = tail(logfile) last_line = tail(logfile, window=1)
logged_msg = last_line.split(" - ")[1] logged_msg = last_line.split(" - ")[1]
self.assertEqual(test_msg, logged_msg) self.assertEqual(test_msg, logged_msg)
def tail( f, window=20 ):
"""
from
http://stackoverflow.com/questions/136168/get-last-n-lines-of-a-file- \
with-python-similar-to-tail
"""
BUFSIZ = 1024
f.seek(0, 2)
bytes = f.tell()
size = window
block = -1
data = []
while size > 0 and bytes > 0:
if (bytes - BUFSIZ > 0):
# Seek back one whole BUFSIZ
f.seek(block*BUFSIZ, 2)
# read BUFFER
data.append(f.read(BUFSIZ))
else:
# file too small, start from begining
f.seek(0,0)
# only read what was not read
data.append(f.read(bytes))
linesFound = data[-1].count('\n')
size -= linesFound
bytes -= BUFSIZ
block -= 1
return '\n'.join(''.join(data).splitlines()[-window:])
+32
View File
@@ -3,6 +3,7 @@ Small classes to assist with timezone calculations, LOGGER configuration,
and other common operations. and other common operations.
""" """
import logging import logging
import logging.config import logging.config
from os.path import join, abspath, dirname from os.path import join, abspath, dirname
@@ -17,3 +18,34 @@ def logger_path():
import zipline import zipline
log_path = dirname(abspath(zipline.__file__)) log_path = dirname(abspath(zipline.__file__))
return join(log_path, 'logging.cfg') return join(log_path, 'logging.cfg')
# utility for tailing a log file.
def tail( f, window=20 ):
"""
from
http://stackoverflow.com/questions/136168/get-last-n-lines-of-a-file- \
with-python-similar-to-tail
"""
BUFSIZ = 1024
f.seek(0, 2)
bytes = f.tell()
size = window
block = -1
data = []
while size > 0 and bytes > 0:
if (bytes - BUFSIZ > 0):
# Seek back one whole BUFSIZ
f.seek(block*BUFSIZ, 2)
# read BUFFER
data.append(f.read(BUFSIZ))
else:
# file too small, start from begining
f.seek(0,0)
# only read what was not read
data.append(f.read(bytes))
linesFound = data[-1].count('\n')
size -= linesFound
bytes -= BUFSIZ
block -= 1
return '\n'.join(''.join(data).splitlines()[-window:])