Change variable names and streamline test method
This commit is contained in:
parent
22a8c6efcf
commit
90d9f17468
|
@ -190,16 +190,16 @@ def enable_pretty_logging(options=None, logger=None):
|
|||
logger = logging.getLogger()
|
||||
logger.setLevel(getattr(logging, options.logging.upper()))
|
||||
if options.log_file_prefix:
|
||||
if options.rotating_mode == 'sized':
|
||||
if options.log_rotate_mode == 'size':
|
||||
channel = logging.handlers.RotatingFileHandler(
|
||||
filename=options.log_file_prefix,
|
||||
maxBytes=options.log_file_max_size,
|
||||
backupCount=options.log_file_num_backups)
|
||||
elif options.rotating_mode == 'timed':
|
||||
elif options.log_rotate_mode == 'time':
|
||||
channel = logging.handlers.TimedRotatingFileHandler(
|
||||
filename=options.log_file_prefix,
|
||||
when=options.log_file_rotating_when,
|
||||
interval=options.log_file_rotating_interval,
|
||||
when=options.log_rotate_when,
|
||||
interval=options.log_rotate_interval,
|
||||
backupCount=options.log_file_num_backups)
|
||||
else:
|
||||
channel = logging.NullHandler()
|
||||
|
@ -244,13 +244,13 @@ def define_logging_options(options=None):
|
|||
options.define("log_file_num_backups", type=int, default=10,
|
||||
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 "
|
||||
"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")
|
||||
|
||||
options.define("rotating_mode", type=str, default='sized',
|
||||
help="The mode of rotating files(timed or sized)")
|
||||
options.define("log_rotate_mode", type=str, default='size',
|
||||
help="The mode of rotating files(time or size)")
|
||||
|
||||
options.add_parse_callback(lambda: enable_pretty_logging(options))
|
||||
|
|
|
@ -165,25 +165,16 @@ class EnablePrettyLoggingTest(unittest.TestCase):
|
|||
tmpdir = tempfile.mkdtemp()
|
||||
try:
|
||||
self.options.log_file_prefix = tmpdir + '/test_log'
|
||||
self.options.rotating_mode = 'timed'
|
||||
self.options.log_file_rotating_when = 'S'
|
||||
self.options.log_file_rotating_interval = 1
|
||||
self.options.log_rotate_mode = 'time'
|
||||
enable_pretty_logging(options=self.options, logger=self.logger)
|
||||
self.logger.error('hello1 from TimedRotatingFileHandler')
|
||||
time.sleep(1.01)
|
||||
self.logger.error('hello2 from TimedRotatingFileHandler')
|
||||
self.logger.error('hello')
|
||||
self.logger.handlers[0].flush()
|
||||
filenames = glob.glob(tmpdir + '/test_log*')
|
||||
self.assertEqual(2, len(filenames))
|
||||
new_file = tmpdir + '/test_log'
|
||||
old_file = glob.glob(tmpdir + '/test_log.*')[0]
|
||||
with open(old_file) as f:
|
||||
self.assertEqual(1, len(filenames))
|
||||
with open(filenames[0]) as f:
|
||||
self.assertRegexpMatches(
|
||||
f.read(),
|
||||
r'^\[E [^]]*\] hello1 from TimedRotatingFileHandler$')
|
||||
with open(new_file) as f:
|
||||
self.assertRegexpMatches(
|
||||
f.read(),
|
||||
r'^\[E [^]]*\] hello2 from TimedRotatingFileHandler$')
|
||||
r'^\[E [^]]*\] hello$')
|
||||
finally:
|
||||
for handler in self.logger.handlers:
|
||||
handler.flush()
|
||||
|
|
Loading…
Reference in New Issue