Change variable names and streamline test method

This commit is contained in:
Ping 2015-08-11 13:41:44 +08:00
parent 22a8c6efcf
commit 90d9f17468
2 changed files with 14 additions and 23 deletions

View File

@ -190,16 +190,16 @@ def enable_pretty_logging(options=None, logger=None):
logger = logging.getLogger() logger = logging.getLogger()
logger.setLevel(getattr(logging, options.logging.upper())) logger.setLevel(getattr(logging, options.logging.upper()))
if options.log_file_prefix: if options.log_file_prefix:
if options.rotating_mode == 'sized': if options.log_rotate_mode == 'size':
channel = logging.handlers.RotatingFileHandler( channel = logging.handlers.RotatingFileHandler(
filename=options.log_file_prefix, filename=options.log_file_prefix,
maxBytes=options.log_file_max_size, maxBytes=options.log_file_max_size,
backupCount=options.log_file_num_backups) backupCount=options.log_file_num_backups)
elif options.rotating_mode == 'timed': elif options.log_rotate_mode == 'time':
channel = logging.handlers.TimedRotatingFileHandler( channel = logging.handlers.TimedRotatingFileHandler(
filename=options.log_file_prefix, filename=options.log_file_prefix,
when=options.log_file_rotating_when, when=options.log_rotate_when,
interval=options.log_file_rotating_interval, interval=options.log_rotate_interval,
backupCount=options.log_file_num_backups) backupCount=options.log_file_num_backups)
else: else:
channel = logging.NullHandler() channel = logging.NullHandler()
@ -244,13 +244,13 @@ def define_logging_options(options=None):
options.define("log_file_num_backups", type=int, default=10, options.define("log_file_num_backups", type=int, default=10,
help="number of log files to keep") help="number of log files to keep")
options.define("log_file_rotating_when", type=str, default='midnight', options.define("log_rotate_when", type=str, default='midnight',
help=("specify the type of TimedRotatingFileHandler interval " help=("specify the type of TimedRotatingFileHandler interval "
"other options:('S', 'M', 'H', 'D', 'W0'-'W6')")) "other options:('S', 'M', 'H', 'D', 'W0'-'W6')"))
options.define("log_file_rotating_interval", type=int, default=1, options.define("log_rotate_interval", type=int, default=1,
help="The interval value of timed rotating") help="The interval value of timed rotating")
options.define("rotating_mode", type=str, default='sized', options.define("log_rotate_mode", type=str, default='size',
help="The mode of rotating files(timed or sized)") help="The mode of rotating files(time or size)")
options.add_parse_callback(lambda: enable_pretty_logging(options)) options.add_parse_callback(lambda: enable_pretty_logging(options))

View File

@ -165,25 +165,16 @@ class EnablePrettyLoggingTest(unittest.TestCase):
tmpdir = tempfile.mkdtemp() tmpdir = tempfile.mkdtemp()
try: try:
self.options.log_file_prefix = tmpdir + '/test_log' self.options.log_file_prefix = tmpdir + '/test_log'
self.options.rotating_mode = 'timed' self.options.log_rotate_mode = 'time'
self.options.log_file_rotating_when = 'S'
self.options.log_file_rotating_interval = 1
enable_pretty_logging(options=self.options, logger=self.logger) enable_pretty_logging(options=self.options, logger=self.logger)
self.logger.error('hello1 from TimedRotatingFileHandler') self.logger.error('hello')
time.sleep(1.01) self.logger.handlers[0].flush()
self.logger.error('hello2 from TimedRotatingFileHandler')
filenames = glob.glob(tmpdir + '/test_log*') filenames = glob.glob(tmpdir + '/test_log*')
self.assertEqual(2, len(filenames)) self.assertEqual(1, len(filenames))
new_file = tmpdir + '/test_log' with open(filenames[0]) as f:
old_file = glob.glob(tmpdir + '/test_log.*')[0]
with open(old_file) as f:
self.assertRegexpMatches( self.assertRegexpMatches(
f.read(), f.read(),
r'^\[E [^]]*\] hello1 from TimedRotatingFileHandler$') r'^\[E [^]]*\] hello$')
with open(new_file) as f:
self.assertRegexpMatches(
f.read(),
r'^\[E [^]]*\] hello2 from TimedRotatingFileHandler$')
finally: finally:
for handler in self.logger.handlers: for handler in self.logger.handlers:
handler.flush() handler.flush()