From a48b51721d386eafbc4a07d69990997d70727112 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Fri, 30 Aug 2019 14:56:46 +0300 Subject: [PATCH] Test new logging approach, both debug.logger and resetLogging --- src/debug.py | 6 ++-- src/tests/test_logger.py | 69 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 src/tests/test_logger.py diff --git a/src/debug.py b/src/debug.py index 7d523b3c..472b0d02 100644 --- a/src/debug.py +++ b/src/debug.py @@ -68,7 +68,8 @@ def configureLogging(): fail_msg = '' try: logging_config = os.path.join(state.appdata, 'logging.dat') - logging.config.fileConfig(logging_config) + logging.config.fileConfig( + logging_config, disable_existing_loggers=False) return ( False, 'Loaded logger configuration from %s' % logging_config @@ -80,7 +81,8 @@ def configureLogging(): ' logging config\n%s' % \ (logging_config, sys.exc_info()) else: - # no need to confuse the user if the logger config is missing entirely + # no need to confuse the user if the logger config + # is missing entirely fail_msg = 'Using default logger configuration' logging_config = { diff --git a/src/tests/test_logger.py b/src/tests/test_logger.py new file mode 100644 index 00000000..57448911 --- /dev/null +++ b/src/tests/test_logger.py @@ -0,0 +1,69 @@ +""" +Testing the logger configuration +""" + +import logging +import os +import tempfile +import unittest + + +class TestLogger(unittest.TestCase): + """A test case for bmconfigparser""" + + conf_template = ''' +[loggers] +keys=root + +[handlers] +keys=default + +[formatters] +keys=default + +[formatter_default] +format=%(asctime)s {1} %(message)s + +[handler_default] +class=FileHandler +level=NOTSET +formatter=default +args=('{0}', 'w') + +[logger_root] +level=DEBUG +handlers=default +''' + + def test_fileConfig(self): + """Put logging.dat with special pattern and check it was used""" + tmp = os.environ['BITMESSAGE_HOME'] = tempfile.gettempdir() + log_config = os.path.join(tmp, 'logging.dat') + log_file = os.path.join(tmp, 'debug.log') + + def gen_log_config(pattern): + """A small closure to generate logging.dat with custom pattern""" + with open(log_config, 'wb') as dst: + dst.write(self.conf_template.format(log_file, pattern)) + + pattern = r' o_0 ' + gen_log_config(pattern) + + try: + from pybitmessage.debug import logger, resetLogging + if not os.path.isfile(log_file): # second pass + pattern = r' <===> ' + gen_log_config(pattern) + resetLogging() + except ImportError: + self.fail('There is no package pybitmessage. Things gone wrong.') + finally: + os.remove(log_config) + + logger_ = logging.getLogger('default') + + self.assertEqual(logger, logger_) + + logger_.info('Testing the logger...') + + self.assertRegexpMatches(open(log_file).read(), pattern)