diff --git a/python/nistoar/base/config.py b/python/nistoar/base/config.py index 6a6f7be..3c40a6f 100644 --- a/python/nistoar/base/config.py +++ b/python/nistoar/base/config.py @@ -153,7 +153,7 @@ def configure_log(logfile: str=None, level: int=None, format: str=None, config: level = _log_levels_byname.get(str(level), level) if not isinstance(level, int): raise ConfigurationException("Unrecognized loglevel value: "+str(level)) - + if not format: format = config.get('logformat', LOG_FORMAT) frmtr = logging.Formatter(format) @@ -180,6 +180,14 @@ def configure_log(logfile: str=None, level: int=None, format: str=None, config: if level >= logging.DEBUG: logging.getLogger("filelock").setLevel(level+10) + # config can set levels on a per-logname basis + if config.get("loglevelsfor") and isinstance(config.get("loglevelsfor"), Mapping): + for lognm, level in config.get("loglevelsfor", {}).items(): + if not isinstance(level, int): + level = _log_levels_byname.get(str(level), level) + if isinstance(level, int): + logging.getLogger(lognm).setLevel(level) + if addstderr: if not isinstance(addstderr, str): addstderr = format diff --git a/python/tests/nistoar/base/test_config.py b/python/tests/nistoar/base/test_config.py index 86d12eb..0f624c7 100644 --- a/python/tests/nistoar/base/test_config.py +++ b/python/tests/nistoar/base/test_config.py @@ -122,7 +122,10 @@ def test_from_config(self): cfg = { 'logdir': tmpd, 'logfile': logfile, - 'loglevel': 'DEBUG' + 'loglevel': 'DEBUG', + 'loglevelsfor': { + 'pymongo': 'INFO' + } } self.logfile = os.path.join(tmpd, logfile) @@ -132,11 +135,17 @@ def test_from_config(self): self.assertEqual(config.global_logdir, tmpd) self.assertEqual(config.global_logfile, self.logfile) + self.assertEqual(self.rootlog.getEffectiveLevel(), logging.DEBUG-1) + self.assertEqual(config._log_handler.level, logging.DEBUG) + self.assertEqual(logging.getLogger("pymongo").getEffectiveLevel(), logging.INFO) + self.rootlog.warning('Oops') self.assertTrue(os.path.exists(self.logfile)) with open(self.logfile) as fd: words = fd.read() self.assertIn("Oops", words) + + def test_abs(self): self.logfile = os.path.join(tmpd, "cfgfile.log")